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. http://msdn.microsoft.com/en-us/library/dd465326.aspx

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])

image

image

· Create a Web.Computername.config and perform the default transformations as described here http://msdn.microsoft.com/en-us/library/dd465326.aspx

image

  • Unload your project and edit the file

image

image

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

   2:  

   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>

   7:  

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 http://stackoverflow.com/questions/3004210/app-config-transformation-for-projects-which-are-not-web-projects-in-visual-studi 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

https://skydrive.live.com/embedicon.aspx/WordPress%20Files/Transformations.zip?cid=061e84eb408bad04&sc=documents

Advertisements

No comments yet... Be the first to leave a reply!

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

%d bloggers like this: