Best Approach For Configuring Multiple .Net Applications

为君一笑 提交于 2020-01-19 14:23:19

问题


We have a suite of interlinked .Net 3.5 applications. Some are web sites, some are web services, and some are windows applications. Each app currently has its own configuration file (app.config or web.config), and currently there are some duplicate keys across the config files (which at the moment are kept in sync manually) as multiple apps require the same config value. Also, this suite of applications is deployed across various envrionemnts (dev, test, live etc)

What is the best approach to managing the configuration of these multiple apps from a single configuration source, so configuration values can be shared between multiple apps if required? We would also like to have separate configs for each environment (so when deploying you don't have to manually change certain config values that are environment specific such as conenction strings), but at the same time don't want to maintain multiple large config files (one for each environment) as keeping this in sync when adding new config keys will prove troublesome.


回答1:


We use file templates such as MyApp.config.template and MyWeb.config.template with NAnt properties for the bits that are different between environments. So the template file might look a bit like this:

<MyAppConfig>
    <DbConnString>${DbConnString}</DbConnString>
    <WebServiceUri uri="${WebServiceUri}" />
</MyAppConfig>

During a build we generate all the configs for the different environments by just looping through each environment in a NAnt script, changing the value of the NAnt properties ${DbConnString} and ${WebServiceUri} for each environment (in fact these are all set in a single file with sections for each environment), and doing a NAnt copy with the option to expand properties turned on.

It took a little while to get set up but it has paid us back at least tenfold in the amount of time saved messing around with different versions of config files.




回答2:


Visual Studio has a relatively obscure feature that lets you add existing items as links, which should accomplish what you're looking for. Check out Derik Whittaker's post on this topic for more detail.

Visual Studio really should make this option more visible. Nobody really thinks to click on that little arrow next to the "Add" button.




回答3:


You can split App.config into multiple configuration files. You just specify the name of the file that contains the config section.

Change app.config:

<SomeConfigSection>
  <SettingA/>
  <SettingB/>
</SomeConfigSection>
<OtherSection>
  <SettingX/>
</OtherSection>

Into app.config and SomeSetting.xml:

<SomeConfigSection file="SomeSetting.xml" />
<OtherSection file="Other.xml" />

Where SomeSetting.xml contains:

Now you can compose your app.config from different section files with some sort of build or deploy script. E.g.:

if debug copy SomeSettingDebug.xml deploydir/SomeSetting.xml
if MySql copy OtherSectionMySql.xml deploydir/OtherSetting.xml



回答4:


These 2 questions might help you: Utilizing machine.config and Managing app.config for large projects




回答5:


Check out the prism framework from Microsofts patterns and practices group?



来源:https://stackoverflow.com/questions/96340/best-approach-for-configuring-multiple-net-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!