How to transform web config or app config by environment in visual studio

1 minute read

In software development, there are at least several environments to manage: development (DEV), staging (STAG or PRE), and production (PRD).

There will be integration (INT), user acceptance testing (UAT) in a more complete environment configuration.

It could be frustrating when managing all those environments because a small error in the deployment could generate an incident, or disaster!

Fortunately there is a cool tool to facilitate the environment management and software deployment in visual studio.

It is a visual studio extension: Configuration Transform

Once you have installed Configuration Transform, you can create environment configurations for your project.

And then you can location the app.config, right click on it and then choose Add config transforms.

You can see the following configuration files for DEV and INT.

Now, I have prepared some code to ensure the correct config file is used when running the program.

And with the App.config:

To override the default configuration for INT environment, ApplicationName is replaced in App.INT.config.

The following MSBuild command will build the solution, and generate the binaries with the correct App.config.

> MSBuild.exe NetDemo.sln /property:Configuration=INT -target:clean;compile;build

You can see App.INT.config is used to replace the default configuration of App.config, and generate the final NetDemo.exe.config.

You can see the ApplicationName is “NetDemo INT” when the program run.

You can download the source code HERE

SUN Jiangong

SUN Jiangong

A senior .NET engineer, software craftsman. Passionate about new technologies.