Monday, September 26, 2011

Visual Studio 2010 - Add Config Transforms


When it comes to deploying applications to various environments such as test and production, we typically need different configuration settings. Those who prefer build/deployment scripts, often utilize their scripts to create configuration files for necessary environments. Visual Studio 2010 has a new feature only for Web Applications to create configurations for various environments.


 
For our Web Application projects, if we right click on web.config file, we see a menu option called “Add Config Transforms”. As soon as we click this option, Visual Studio creates new configuration files based on the solution configuration. An icon immediately shows up next to the Web.config file, and we can click this to expand and view the newly created files. Since, Visual Studio is typically configured with Debug and Release modes, we may see Web.Debug.config and Web.Release.config files. If we want configurations for additional environments, we need to open up solution properties, and click the "Configuration Manager" button from the "Configuration Properties". Now, from the “Active solution configuration” drop-down, we can click “<New...>” option. This allows us to add information about custom environments in addition to Debug and Release modes. Once added, we can click the “Add Config Transforms” once again. This time we see the new configuration file(s) just like Web.Debug.config and Web.Release.config files.

So, what to do with these new files? We can essentially add replacement scripts inside these files to alter a specific configuration element for individual environments. Here's a couple of examples from Web.Release.config,

<connectionStrings>
<add name="MyConnectionString"
connectionString="my production connection string" providerName="System.Data.SqlClient"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

<appSettings>
<add key ="SomeSetting" value ="Production Value" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>


Now, if we compile solution in Release mode, the above script will alter the output configuration as specified above. Fairly straightforward..... right?

I definitely love this new feature; but it's only available for Web Application projects. This is not available for other projects like Console Applications, WPF, Silverlight and Windows Form Projects. I simply don't understand why this feature was added only for Web Application projects. Hopefully, Microsoft will soon expand this feature for other projects as well.

No comments:

Post a Comment