Click here to Skip to main content
15,851,503 members
Articles / Web Development / HTML

How to transform different config file without any third party extension.

Rate me:
Please Sign up or sign in to vote.
4.93/5 (10 votes)
29 May 2016CPOL2 min read 24.2K   256   15   1
Configuration transformation for different environment.

Introduction

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 appear 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
    XML
        <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.
    XML
      <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
<?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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Bangladesh Bangladesh
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.

Comments and Discussions

 
Questionfor SDK projects Pin
Member 30036368-Sep-23 5:31
Member 30036368-Sep-23 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.