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.

How transformation work?

Create a console app 'ConfigurationTransform' from visual studio.
 
  • From solution configuration menu click 'Configuration Manager'
  • From Configuration manager click 'Active solution configuration' drop down and click new.
  • Set the name of the configuration like 'Dev' and select Copy settings from as 'Release' and then click ok.
  • Open project directory by right-clicking on the project and click 'Open Folder in File Explorer'
  • Now copy the App.config file and paste in the same directory and rename it to App.Dev.config.
  • Unload the project by right-clicking on the project and click 'Unload project'. If save change prompt appeared then click yes.
  • Right-click on the project and click on 'Edit ConfigurationTransform.csproj'
  • Find the Item group 'None Include="App.config"' and add another code block just below that as
        <None Include="App.Dev.config">
          <DependentUpon>App.config</DependentUpon>
        </None>
  • 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.
      <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
      <Target Name="AfterCompile" Condition="Exists('App.$(Configuration).config')">
        <!--Generate transformed app config in the intermediate directory-->
        <TransformXml Source="App.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.$(Configuration).config" />
        <!--Force build process to use the transformed configuration file from now on.-->
       <ItemGroup>
          <AppConfigWithTargetPath Remove="App.config" />
          <AppConfigWithTargetPath Include="$(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'
 
Now add appsettings block in configuration section in App.config as 
<appSettings>
    <add key="adminEmail" value="admin@local.com" />
    <add key="serviceUri" value="Local ServiceUri" />
  </appSettings>
Add xml namespace attribute 'xmlns:xdt' in App.Dev.config file as 
<?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="adminEmail" value="admin@dev.com" xdt:Transform="Replace" xdt:Locator="Match(key)" />
    <add key="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