In the Life of a TFS Configuration Manager Part 2: Configuration Transforms in Non-Web Projects

It was great that Microsoft added support for configuration transforms in web projects in Visual Studio 2010, but what about non-web projects like console applications? Well, it can be accomplished but not without some careful editing of the project file. It is described in an MSDN blog called XDT (web.config) Transforms in non-web projects.

Add a new item to the project and choose the Application Configuration File template. Call it e.g. App.Prod.config.

Code your transform. Here is an example:

<?xml version="1.0"?>
    <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 –>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <appSettings>
     <add key="ZipFilesStorage" value="\\INE-PIR-FSCL01\RGS\CountyCouncilAccessLogs" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
   </appSettings>
</configuration>

Do this for all configurations in your project.

I have stored the TransformFiles.targets file (linked from the blog mentioned above) in source control rather than in $(MSBuildExtensionsPath)\Custom (%ProgramFiles (x86)%\MSBuild\Custom). To make sure this file is downloaded when you get the project from source control, add the file as a link in the project.

Edit your project file by right-clicking on the project and choosing Edit Project File.

Add an import of the transform target before the closing </Project> tag. Make sure the number of parent path symbols (..) reflects your folder structure.

  <Import Project="$(ProjectDir)..\..\TransformFiles.targets" />

Replace e.g.

    <None Include="App.AT.config" />
    <None Include="App.config">
        <SubType>Designer</SubType>
    </None>
    <None Include="App.Prod.config" />
    <None Include="App.ST.config" />

with e.g.

    <None Include="App.config">
       <TransformOnBuild>true</TransformOnBuild>
     </None>
     <None Include="App.Dev.config">
       <DependentUpon>App.config</DependentUpon>
     </None>
     <None Include="App.Prod.config">
       <DependentUpon>App.config</DependentUpon>
     </None>
     <None Include="App.AT.config">
       <DependentUpon>App.config</DependentUpon>
     </None>
     <None Include="App.ST.config">
       <DependentUpon>App.config</DependentUpon>
     </None>

This should transform your configuration file both when building using Visual Studio and MSBuild (Team Foundation Server). However, it will not work if you’re using Visual Studio Installer projects to create MSI packages for installation. The next part of this series will cover that.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s