Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps

Adding Transformation Files in the .NET Windows Service

3.70/5 (6 votes)
9 Dec 2016CPOL 19K  
Step by step to add the transformation files of app.config in your .NET Windows service

Introduction

I have tried to make it very simple to add the transformation files of app.config file into .NET Windows Service.

Step by Step

Step 1

Add the transform files manually on the project folder where the app.config is, for example, I have added three transformation files as below.

  • App.FSTTransform.config
  • App.LIVETransform.config
  • App.UATTransform.config

Step 2

Right click on the .csproj project file from the project folder and edit that with any editor.

Step 3

On the opened XML file, replace the below code:

XML
<ItemGroup>
  <None Include="App.config" />
</ItemGroup>

by the below XML:

XML
<ItemGroup>
   <None Include="App.config">
     <SubType>Designer</SubType>
   </None>
   <None Include="App.FSTTransform.config">
     <DependentUpon>
       App.config
     </DependentUpon>
   </None>
   <None Include="App.UATTransform.config">
     <DependentUpon>
       App.config
     </DependentUpon>
   </None>
   <None Include="App.LIVETransform.config">
     <DependentUpon>
       App.config
     </DependentUpon>
   </None>
 </ItemGroup>

Step 4

Add below XML target after the Import <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />:

XML
<Target Name="AfterCompile">
    <TransformXml Source="App.config"
    Destination="App_FST.config" Transform="App.FSTTransform.config" />
    <TransformXml Source="App.config"
    Destination="App_UAT.config" Transform="App.UATTransform.config" />
    <TransformXml Source="App.config"
    Destination="App_LIVE.config" Transform="App.LIVETransform.config" />
</Target>

Step 5

Define the TransformXML on before the <PropertyGroup> on top of the project XML file as below:

XML
<UsingTask TaskName="TransformXml"
AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.Publishing.Tasks.dll" /> 

Final Step

Go to Visual Studio and reload the project. Build the project.

License

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