开发者

Choose settings based on machine name

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.


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"/>


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"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜