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):
<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 .