Override appSettings with the file attribute

To help make applications a bit more portable, it is nice to know you can override the appSettings group in your app.config or web.config file in the following manner:

<appSettings file=”userAppSettings.config”>
    <add key=”mysetting” value=”mysetting_value” />
    <add key=”mysetting_2″ value=”mysetting_value_2″ />
appSettings>

The, in the same directory, create a file userAppSettings.config, and place the settings you want to override within:

<appSettings>
    <add key=”mysetting” value=”mysetting_value” />
appSettings>

Notice there is no file attribute here.  Also, note that you can change the location of the file; it doesn’t neccessarily have to be in the same directory as your app.config/web.config.

If you don’t override a setting in your userAppSettings.config file, the “default” value from the base file will be used in its stead.

Too bad this is the only configuration section that supports the file attribute 🙁 …

Triggering Application Restarts with your File Override

One thing you may notice is that a change to your userAppSettings.config file doesn’t restart your web application and reload the configuration as would happen when you touch the web.config.  To change this behaviour for all your web applications, edit your machine.config and change:

restartOnExternalChanges=”false”

to

restartOnExternalChanges=”true”

in the configuration section declaration line

<section name=”appSettings” type=”System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” restartOnExternalChanges=”true” requirePermission=”false” />

Now, every time you touch a file override, the application will restart for all your web applications on that machine.

This entry was posted in C#. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *