Using Config Transformation for easier (Azure) Development

Visual Studio 2010 has this nice feature for config transformations. When adding a new build configuration you also get the possibility to add a web.[Configuration].config file which contains specific settings for that specific configuration.

When using the publish function, the transformation uses pretty slick XSLT transformations to update the web.config with the settings you specified in the web.[configuration].config.

You can read all about this functionality here.

When you are coding for Azure or coding in a team with a number of developers, web.configs and app.configs can really make life hard. Especially for Azure it is hard to maintain the different configurations for the Acceptance and Production environment.

Likewise when you are developing software in a team, it is also very unhandy that with every Get Latest Version, your config settings with all your connectionstrings and local paths are overwritten with the settings of your fellow coder.

Would it not be nice to use these transformations to solve these problems as well? Just create your own config settings and the build process takes care of everything? Don’t worry about wrong settings or connections? Yes it would ! Unfortunately, the transformations only work on publish and not on build.

Together with my friend and colleague from Delta-N Jasper Gilhuis, I figured out a way to use this transform functionality within Visual Studio and the TFS Build Server, which gives you loads of possibilities regarding different configuration files.

The solution we have found can be implemented in the following way:

  • Create a build configuration for your local machine (for example [ComputerName])



· Create a Web.Computername.config and perform the default transformations as described here


  • Unload your project and edit the file



  • Add this piece of code at the end of the project file.
   1: <UsingTask AssemblyFile="..\BuildTasks\Microsoft.Web.Publishing.Tasks.dll" TaskName="TransformXml" />


   3: <Target Name="BeforeBuild">

   4:   <Copy SourceFiles="Web.Config" DestinationFiles="Web.Original.Config" />

   5:   <TransformXml Source="Web.Original.Config" Transform="Web.$(Configuration).config" Destination="Web.config" />

   6: </Target>


Make sure the path to the Microsoft.Web.Publishing.Tasks.Dll can be found. Best is to

copy the assembly to a relative folder of your solution. You can find the assembly here. C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web

  • Copy all files to local directory
  • Reload your solution
  • You’re done.

To test it, just put a dummy value in your web.config and a real value in your “own” config. Build Solution and voila!

For the Team Foundation Build Server, just create a configuration like you did before and point to that configuration in the Build Dialog.

Nice thing is, this works for all xml based config files because it is plain XSLT transformations. To use this functionality with app.config or any other.config, follow the steps described here to make the other configs dependent as well.

Hope this helps!

For your convenience I added a zip with a preconfigured solution with web.config transformations

Comments are closed.

%d bloggers like this: