Anyone who has ever had to manage multiple configuration files for different environments will appreciate this. This is an attempt to simplify the multi-configuration file problem by using some code generation.

The problems with keeping multiple versions of config files are:

  1. When adding a new setting to a config file, developers will forget to add it to all config files. The issue doesn’t show up until the config file is used. This can cause a state where you can’t find an issue until it has gone to production.
  2. Understanding what is different between configuration files is tedious, time consuming work. To find the differences between web.config.qa and web.config.prod, you have to use a diff tool like beyond compare to visualize the differences.

The solution is to not maintain versions manually, instead track only the things that are different and use a single master template to inject environment specific values into config files.

Using a spreadsheet allows us to track only the values that are different between environments.

This solution uses the T4Toolbox to generate the web.config’s from a template. There are actually two templates used here, one is a template for the web.config and one is a generator that processes the spreadsheet using the web.config template.

The WebConfigTempalte.tt template will loop though each environment and create a environment specific file. The values read from the spreadsheet are injected into the environment specific configuration file.

WebConfigGenerator.tt is the template that loops through the values in the spreadsheet and injects them into the web.config files.

The generated web.config files are automatically included in your visual studio project.

If you have a tool like Visual SVN for visual studio, you can instantly check the new files into source control.

From now on all you have to do is update values in the spreadsheet and let the code generation process handle generation of the web.configs in a consistent and reliable manner. Much better than doing it manually!