views:

19

answers:

2

When we deploy applications, there is usually a separate machine for production and development. Most of the applications have settings in a regular .NET App.config or Web.config file. For example:

<add key="Dev_Setting1" value="val1"/>
<add key="Prod_Setting1" value="val2"/>

<add key="Prod_Setting1" value="val3"/>
<add key="Prod_Setting2" value="val4"/>

We'd like to have the applications choose automatically between two sets of settings, depending on the machine. Is there a standard way of doing this? Other suggestions welcome.

+1  A: 

How about the following approach?

Having the machine name in one of the settings would allow you to determine which prefix needs to be used to load up the application settings - so long as you avoid loading through the standard API and instead implement your own 'get me a setting value' you can lookup the setting prefix from the machine name and then look up the relevant setting.

<add key="MachineConfigPrefix_MyMachine1" value="Prod"/>
<add key="MachineConfigPrefix_MyMachine2" value="Dev"/>

<add key="Prod_Setting1" value="val3"/>
<add key="Prod_Setting2" value="val4"/>

<add key="Dev_Setting1" value="val5"/>
<add key="Dev_Setting2" value="val6"/>
Will A
+1  A: 

I usually use a method that first looks for a setting with the machine name as prefix:

public static string GetConfiguration(string key) {
  return ConfigurationManager.AppSettings[Environment.MachineName + "." + key] ?? ConfigurationManager.AppSettings[key];
}

That way you can have a default setting, and local settings for any machine:

<add key="Setting1" value="val1"/>
<add key="Developer1.Setting1" value="val2"/>
<add key="Developer2.Setting1" value="val3"/>
<add key="TestServer.Setting1" value="val4"/>
Guffa