Niraj Bhatt – Architect's Blog

Ruminations on .NET, Architecture & Design

Integrating Web Config Transformations with TFS Build

Web Config Transformations a new feature available with .NET Framework 4.0 allows us to alter the configuration settings depending on the application target environment. Transformations are specified using Locator and Transform attributes within your configuration elements. You need to qualify these transformations with corresponding XML namespace as shown below (extract of sample web.staging.config):

<configuration xmlns:xdt=”;>
     <add name=”DBConnString” … xdt:Transform=”Replace” xdt:Locator=”Match(name)”

It’s important to note the outer elements (configuration/connectionStrings) which form the effective path of the XPath Expression. If Locator is missing implicit path is considered of the current element is considered which needs to be transformed. Locator attribute provides operations like Condition, Match and XPath to locate elements. Transform attribute provides operations like Replace, Insert, InsertBefore, InsertAfter, Remove, RemoveAll, RemoveAttributes, and SetAttributes among others.

Ideal productivity booster would be to integrate these transforms into our TFS Builds. So we select a build configuration and trigger a build and build picks up the corresponding transforms to be applied. Let’s see how we can do that.

To start with you need to add a new Solution Configuration using Configuration Manager.

Right click the web.config and select ‘Add Config Transforms’

You can use the newly created file – ‘Web.Staging.Config’ to add all the transformations. Next step is to create a new TFS Build definition. While creating a new definition you need to specify ‘Staging’ for configuration build (this has to be a manual entry as the drop down would only support Debug / Release). We also pass an argument to via build definition (IsTFSBuild) in this case. Provide all the entries as appropriate inside the build definition.

Final step is to modify your CSPROJ file (you could have modify the BuildTemplate workflow but that’s not for faint hearted like me). Add the MSBuild target element as below which checks the ‘IsTFSBuild’ flag and applies the necessary transformations

<Target Name=”AfterBuild” Condition=”$(IsTFSBuild)==’True'”>
      <DeleteAfterBuild Include=”$(WebProjectOutputDir)\Web.*.config” />
    <TransformXml Source=”Web.config” Transform=”$(ProjectConfigTransformFileName)” Destination=”$(WebProjectOutputDir)\Web.config” />
    <Delete Files=”@(DeleteAfterBuild)” />

You may not find necessary MSBuild tasks like ‘TransformXml’ on your build machine. You would have to copy the below shown files to the MSBuild directory

Hope this sets you up for higher levels of productivity 🙂 .


4 responses to “Integrating Web Config Transformations with TFS Build

  1. tednybergTed Nyberg September 15, 2011 at 11:50 pm

    Great! This pointed me in the right direction, allowing me to solve my problem! Not sure why so many MSBuild tasks aren’t included when MSBuild is installed as part of TFS Build Server, though?

    Thanks again!

  2. Derdour January 3, 2012 at 10:21 pm

    Great! works fine. I don’t know why it doesn’t documented by Microsoft!

  3. Craig Holdheide (@craigholdheide) June 9, 2012 at 1:18 am

    Great blog post. I ran into one issue with this method using the AfterBuild target. I am also using MSDeploy to package and deploy my web application to IIS 7 web server. Your process works but I noticed that when my MSDeploy package gets created it still includes both my Web.Debug.config and Web.Release.config. Any way around this issue?

  4. Jamfish March 16, 2013 at 3:44 am

    This really hit the spot! Thank you!

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: