Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top