Click here to Skip to main content
15,887,898 members
Articles / DevOps
Tip/Trick

Adding Transformation Files in the .NET Windows Service

Rate me:
Please Sign up or sign in to vote.
3.70/5 (6 votes)
9 Dec 2016CPOL 18.1K   6   6
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)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
MCT, MCSD

Comments and Discussions

 
QuestionMissing detail about transform file Pin
Mauro Ciaccio10-Jul-18 5:04
Mauro Ciaccio10-Jul-18 5:04 
GeneralMy vote of 5 Pin
Rahul AnilKumar Mehta13-Dec-16 2:37
Rahul AnilKumar Mehta13-Dec-16 2:37 
Questiongreat work Pin
Rahul AnilKumar Mehta13-Dec-16 2:37
Rahul AnilKumar Mehta13-Dec-16 2:37 
GeneralWeak article Pin
Thornik12-Dec-16 12:33
Thornik12-Dec-16 12:33 
QuestionExplain please Pin
RickZeeland9-Dec-16 9:01
mveRickZeeland9-Dec-16 9:01 
AnswerRe: Explain please Pin
L Hills12-Dec-16 3:24
L Hills12-Dec-16 3:24 
VERY roughly. Lets say you have some code that needs to connect to a resource, maybe a database. You can use transforms so that the output app.config has values appropriate to the build (so a dev build connects to the dev database). See this article on MSDN.

This functionality is built in to the new asp.net templates for web.config, but this article is explaining how to achieve the same thing for non-web projects.

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.