Search form

You are here

This article shows how to automate the process of transforming the configuration file like App.config, Web.config file when we deploy it to the different destination environments like Dev, Stg, Prod. Most applications have settings in the App.config or Web.config file that must be different when the application is deployed. Automating the process of making these changes saves us from having to do them manually every time we deploy, which would be tedious and error prone.

Background

In most real-world scenarios, the configuration (app.config, web.config) file we use for development is different than the one we use for production deployment. Typically we want to change environment settings from the development environment to the production environment. From .Net 4.0 XDT Transformation is come into play to do this type of transformation.

Now add this block of code just before '</Project> tag. We need to run this 'Microsoft.Web.Publishing.Tasks.dll' MSbuild task to do transformation.

HideCopy Code

<UsingTaskTaskName="TransformXml"AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll"/><TargetName="AfterCompile"Condition="Exists('App.$(Configuration).config')"><!--Generate transformed app config in the intermediate directory--><TransformXmlSource="App.config"Destination="$(IntermediateOutputPath)$(TargetFileName).config"Transform="App.$(Configuration).config"/><!--Force build process to use the transformed configuration file from now on.--><ItemGroup><AppConfigWithTargetPathRemove="App.config"/><AppConfigWithTargetPathInclude="$(IntermediateOutputPath)$(TargetFileName).config"><TargetPath>$(TargetFileName).config</TargetPath></AppConfigWithTargetPath></ItemGroup></Target>

Now reload the project by right-clicking on the project and click on 'Reload Project'

<?xmlversion="1.0"?><!-- For more information on using app.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --><configurationxmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"><appSettings><addkey="adminEmail"value="admin@dev.com"xdt:Transform="Replace"xdt:Locator="Match(key)"/><addkey="serviceUri"value="Dev ServiceUri"xdt:Transform="Replace"xdt:Locator="Match(key)"/></appSettings></configuration>

Points of Interest

Now build the solution and go to ../bin/Dev folder and click on 'ConfigurationTransform.exe'. It will print transformed configuration value as

You can add more configuration for different environment like QA, Stg, Prod.

Add new comment

Your name *

E-mail *

The content of this field is kept private and will not be shown publicly.