Separate configuration files per build config in Visual Studio IDE

Generating different configuration files on the build time in VS IDE

  • Share

It is an often case that application configuration is different for each environment. Most often this is the case for database connection string where developer has connection string to dev environment database or it's local database and quality assurance (qa) and production have different connection strings wile the rest of the config. file is the same.

Different config may be also required if you have an app that runs in different geo regions and they access different storages which are located in the same region to reduce latency and therefore application response which is typical for Web applications. In all these cases your application code is the same, it is just different pieces of configuration you need to run them with.

This can be done through different build configuration in Visual Studio and different config transformations can be applied to different build configs. Visual Studio IDE provides this ability throug configuration manager interface. Configuration manager window is available from Build drop-down menu item.

Note

More about how to add build configurations can be found at MSDN online documentation https://msdn.microsoft.com/en-us/library/kwybya3w.aspx

This applies to all types of executable projects in Visual Studio IDE, while there is a different GUI support. For example I found option to generate a different config transformation for ASP.NET project while I did not have such option for console applications or windows service projects.

Even though configuration transformations option is not available for every project type in VS there are community extension which can be installed to solve this. One of them can be found at Visual Studio Market place and it does a great job for this king of requirement https://marketplace.visualstudio.com/items?itemName=GolanAvraham.ConfigurationTransform.

Since App.config as well as Web.config are XML files, transformation is done through XDT. Available transformation options are described in details on MSDN online documentation https://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx.

Running the following sample code on different build configurations will print out the different values.

using System;
using System.Configuration;

namespace ConfigurationTransformSample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(ConfigurationManager.AppSettings["region"]);
            Console.ReadLine();
        }
    }
}

    

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <appSettings>
    <add key="region" value="LOCAL"/>
  </appSettings>
</configuration>
    

We will only use Replace to replace specific AppSetings key value. For the example we will use simple console application and create additional two build definitions using Release build definitions as a template. So we will have the following build definitions in our project:

  • Debug
  • Release
  • Release-EU
  • Release-US

This is first done through Build Configuration window from Build menu and once this is done, transformation files are created from the context menu option on the App.config file which is provided from previously installed extension.

build-config

Once we have different config file, we'll add a key to App.config and then transform it in App.Release-EU.config and App.Release-US.config.

App.Release-EU.config

<?xml version="1.0"?>
<!-- For more information on using app.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="region" value="EUROPE" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>
    

App.Release-US.config

<?xml version="1.0"?>
<!-- For more information on using app.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="region" value="USA" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
  </appSettings>
</configuration>
    

Build configuration can be passed as a parameter to msbuild, so you can use tis to generate different build for different environment if you are setting up Continuous Integration for example in Microsoft Team Services.

References

  • Share

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments for this article

comments powered by Disqus