Select settings based on the machine name.

When deploying applications, there is usually a separate machine for production and development. Most applications have settings in a regular. NET file App.configor Web.config. For instance:

<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 would like applications to automatically choose between two sets of settings, depending on the machine. Is there a standard way to do this? Other suggestions are welcome.

-1
source share
2 answers

What about the following approach?

, - , API " " , .

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

, :

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

, :

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

All Articles