根据机器名称选择设置 [英] Choose settings based on machine name
问题描述
当我们部署应用程序时,通常有一个单独的机器用于生产和开发。大多数应用程序在常规的.NET App.config
或 Web.config
文件中设置。例如:
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.
推荐答案
以下方法如何?
在其中一个设置中使用机器名称将允许您确定需要使用哪个前缀来加载应用程序设置 - 只要您避免通过标准API加载,而是实现您自己的获取设置值可以从机器名称查找设置前缀,然后查找相关设置。
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"/>
这篇关于根据机器名称选择设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!