Wednesday, March 2, 2011

Using Transformation files to transform web.config files for different target deployments in Asp.net 4.0


DARE TO SHARE?
Managing different web.config files for different deployment environment could be a troublesome task and synchronization of these different web.config settings could be hard. 

For example, you may have the following deployment environments for your Asp.net applications for different purpose:

Local : Your own development machine
Development : Where you deploy your codes for QA testing
Staging : Where you deploy your codes for Production testing
Production : Where you deploy your codes for Production

You need to maintain different web.config files for each of these environments, and, when you need to add/delete/modify a particular setting in the main web.config file, you need to do that in all web.config files.

Along with making sure that the web.config files cotains the same set of configuration elements, for some particular configuration elements you may need to put different values for the same settings (Say, each web.config would have different Connection String values).

So, what we usually do is as follows:

1. We maintain different web config files for each different deployment environment which is something like as follows:

web.config (For local development environment)
web.config.dev (For Development site)
web.config.staging (For Staging site)
web.config.release (For Production site)

Whenever we do any change (Add/remove/modify) for a configuration element in the web.config file, we do the same change manually in all three files.

3. We put correct values for some particular configuration element (Say, Connection String) for each different web.config file.

4. Before deployment, we rename the corresponding web.config.* file to web.config and deploy.

That's not a huge amount of work we need to do, but, the problem is, there is a huge risk of getting into a situation where we might have different structure of web.config settings because we update all web.config elements manually.

Fortunately, Asp.net 4.0 has a cool solution to this problem, and, this post is all about it.

Transforming web.config files

Since Asp.net 4.0, it is possible to maintain a single web.config file and different Transformation file to manage the different web.config files for different deployment environment. The Transformation files are used by the Visual Studio to copy the original web.config file and put correct values there for generatubg an web.config file automatically for the target deployment environment.

Let us see how this is possible by following these simple steps:

Create an Asp.net Application project in Visual Studio (2010)


Figure : A sample Asp.net web application project
Go to the Build->Configuration Manager menu and add new Configuration settings Dev and Staging site









Figure : Adding new build settings for Asp.net application project
Once two new configuration settings are added (For Dev and Staging), Right click on the web.config in the solution explorer, and click on "Add Coding Transforms" to add the transformation file for each different settings.

Figure : Add transformation files

Visual Studio will generate the transformation files and the corresponding files will be shown in the solution explorer accordingly


Figure : Generated transformation files
Double click on a transformation file and the transformation file will be shown as follows:


Figure : Generated transformation file
Now, let us see an example of using the Transformation files by setting a Connection String configuration element in the original web.config and managing it's values in one of the transformation files 

Open the Transformation file for Dev site (Web.Dev.Config) in the Visual Studio Editor, and, configure it by uncommenting the <connectionString> setting and specifying an appropriate Connection String property value for the settings


Figure : Specifying connection string property for Dev transformation file
Right click on the solution and click on "Build Deployment Package" to generate the web.config files based upon the Transformation file settings.


Figure : Build Deployment Package to generate web.config files from transformation files
Visual Studio will generate the transformation files within the "obj" directory. However, as is this directory is usually hidden in the solution explorer, you need to click on the "Show All Files" button to see the hidden "obj" folder in the solution explorer.


Figure : Show all files to see hidden files in solution explorer

Expand the "obj" folder as follows to find the generated web.config file from it's transformation file


Figure : Transformed web.config file generated from transformation file
Open the generated Web.config file to verify whether it contains the correct settings specified within the transformation file

Figure : Connection string value generated from transformation file

Where is the magic?

The magic is nothing but the two settings in the Transformation file, which is as follows:

Figure : Transformation Settings
The following two Transformation attribute are needed to be configured in the transformation file to generate the web.config files:

xdt:Transform : This setting instructs the Transformation engine to either Replace/Insert/Delete/Remove or Set an attribute value. In our case, we instructed to Set an attribute values for the current element by specifying the value "SetAttributes".

xdt:Locator : This setting instructs the Transformation engine to find the target element which it needs to transform. In our case, we are instructing it to find an <add> element within the <connectionString> element which has a matching name "DbConnection" in the original Web.config file (And, thereby setting the corresponding connection string value as instructed by the xdt:Transform attribute).

There are many flexible options to transform Web.config files using the xdt:Transform and xdt:Locator element, and, you can learn more on this from here.

I bet, this will make your life a little easier.

1 comments:

Leon Meijer said...

Thanks for your great article

Post a Comment