How To Transform Your App.configs With SLowCheeta And MsBuild

If you run unit tests in your project, then it's very likely that you'll have one or more settings that will need to change depending on which environment you're using, e.g. the build server might need to use a different setting than your local PC.  Out of the box, when we work with web projects, app.configs aren't automatically transformed. To manually tell MsBuild to transform a file you'll need to use 'Microsoft.Web.Publishing.Tasks.dll'.  This is where things get interesting, 'Microsoft.Web.Publishing.Tasks.dll' only gets installed with Visual Studio.  Installing Visual Studio on the build server and paying for a yearly license seems overkill to get access to one assembly, this is where SlowCheeta comes in.

Over the years I've found SlowCheeta works on my local PC brilliantly, but, for whatever reason, it sometimes doesn't seem to transform my app.config magically on the build server.  There is a work around for this, which I'll outline below.  First, make sure you install SlowCheetha into your project using Nuget:

Next, open up your projects '.csproj'.  At the bottom of the file add the bottom four lines:

   <ItemGroup>
    <None Include="app.config">
      <SubType>Designer</SubType>
      <TransformOnBuild>true</TransformOnBuild>
    </None>
    <None Include="app.Debug.config">
      <DependentUpon>app.config</DependentUpon>
      <IsTransformFile>True</IsTransformFile>
      <SubType>Designer</SubType>
    </None>
    <None Include="app.Release.config">
      <DependentUpon>app.config</DependentUpon>
      <IsTransformFile>True</IsTransformFile>
      <SubType>Designer</SubType>
    </None>
    <None Include="packages.config" />
  </ItemGroup>

  <UsingTask TaskName="TransformXml" AssemblyFile="$(SolutionDir)packages\SlowCheetah.2.5.48\tools\SlowCheetah.dll" />
  <Target Name="AfterBuild">
    <TransformXml Source="app.config" Condition="Exists('app.$(Configuration).config')" Transform="app.$(Configuration).config" Destination="app.config" />
  </Target>

One thing that you may need to change is the version number of the SlowCheetah package that you use 2.5.48 is the latest of writing but things change.

 

It is also worth noting that in the image above there is a newer Micosoft.VisualStudio.SlowCheetah package as well.  I havn't used this yet as the old one works, but, you might want to consider using that.
If you add the script above and you have a app.release.config, when you build your project in 'release' mode, as long as the trasnforms have been set-up correctly your app.config should automatically get updated with the latests values, enjoy!

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top