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.
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.
How transformation work?
Create a console app 'ConfigurationTransform' from visual studio.
Now add appsettings block in configuration section in App.config as
<add key="adminEmail" value="email@example.com" />
<add key="serviceUri" value="Local ServiceUri" />
Add xml namespace attribute 'xmlns:xdt' in App.Dev.config file as
<add key="adminEmail" value="firstname.lastname@example.org" xdt:Transform="Replace" xdt:Locator="Match(key)" />
<add key="serviceUri" value="Dev ServiceUri" xdt:Transform="Replace" xdt:Locator="Match(key)" />
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.
I am Towhidul Islam (Tuhin) working as Software Developer. I have 6+ year of experience in .Net technologies. Besides my day to day development work, I love to explore new technologies and write technical article. My area of interest is modern web technologies.