Override appSettings during development

It is a fairly common scenario that during development the local machine configuration settings for different developers in are not same and it may not match with the default value in source control.

One such example can be SQL server connection string. Some developers may have SQL Express installed, others may have a named instance on their local. However, the web.config or app.config can only have one value for this setting.

The way people (at least “I” used to)  handle this usually is to change the configvalues on the local machine during development but discard those changes at the time of commit. If accidentally, however you forget to change the setting back to original, then there is a  risk of breaking the build or code on other developer’s local machine.

Override your appSettings

Recently, my colleague (thanks James!) showed me a nice little trick handle this.  I must say, I was bit embarrassed to not know this already.

You can override the key-value pairs defined under appSettings element of config by using thefile attribute.


<appSettings file="localAppSettings.config">
<add key="dbConnectionString" value="sourceControlConnectionString"/>
<add key="someRandomApplicationSetting" value="sourceControlApplicationSetting"/>
</appSettings>

view raw

web.config

hosted with ❤ by GitHub

The file localAppSettings.config is not part of your source control. If your local settings match exactly as web.config,then you do not need to have this file on your local. Else, you can use this file to override only the settings that are different than web.config.


<appSettings>
<add key="dbConnectionString" value="localMachineConnectionString"/>
</appSettings>

When you use the application settings in your code, you get following values:

Key –> Value
dbConnectionString --> localMachineConnectionString
someRandomApplicationSetting --> sourceControlApplicationSetting

Important Note: If you use connectionStrings element to store your data source connection string, then, you can use configSource attribute instead. The two attributes however, are not equivalent. You can read more about the difference here.

Hope this nice little trick helps ease your development. 🙂

 


Posted

in

by

Comments

2 responses to “Override appSettings during development”

  1. […] on April 20, 2018by admin submitted by /u/vijayankit [link] [comments] No comments […]

    Like

  2. […] back I had written about how to override appSettings during development in traditional ASP.NET […]

    Like

Leave a Reply

A WordPress.com Website.