Config file transformations are a very useful feature supported by VS 2010 web application projects that allow you to maintain config settings for multiple environments, such as dev, staging, production. Unfortunately this feature is only available in web application projects, it is not supported on any other project type out of the box.
This came to my attention as I have been working on a project where we split an existing web application project into 5 separate projects. We moved the EF DataModel into a DAL project, created a BLL class library and separated the UI of the application into 2 web application projects and a website project. In doing so we lost the ability to run config file transformations on 3 out of the 5 projects that made up the solution. This was causing us some amount of headache due to the fact that we are using a non POCO EF model throughout our Web / BLL / DAL projects and we need to keep our EF DataModel connection strings in app.config to facilitate debugging and integration testing.
So I did a bit of research on how to best handle this situation and I found a Visual Studio extension called Slow Cheetah that allows you to run XML transforms on any config file in any type of project. Perfect! It even runs the transformations on build as well as on publish which can save a bit of time.
Now while this does work great I did find it a bit tricky to get it configured for each project type as the configuration settings are slightly different from project to project. I have outlined what worked for me below. I hope this helps someone else out there while working through this type of setup.
For Class Library Projects and Test Projects:
Set your app.config build action to ‘resource’ and copy to output directory to ‘copy always‘ on your app.config properties screen. Building the project will copy the transformed app.config file to your output directory which should be /bin/[configurationname] where configuration name is your currently selected configuration.
For Website Projects:
SlowCheetah does not work with website projects. As a workaround / hack I have been adding a [website].config file to one of my web application projects that contains all config for my website and running the transforms from that project.
For Web Application Projects:
Set your web.config build action to ‘content’ and copy to output directory to ‘copy always’. Web application transforms are executed when you Publish the project.
You can find Slow Cheetah here:
If you want to dig a bit deeper you can also read a great post by Scott Hanselman.