Configuration Transformation
As we
know in application require different configuration values for different
environment. For example, you may have different database, mail or any other setting
in development/staging/production environment. To manage this, person who did deployment
takes need to take care of this configuration value replacement. It’s hard to
manage if config value keeps on changing and there are chances of error.
Visual
studio 2010 provides you option to create transform file for each environment
and its value changed when it is deployed based on selected deployment option.
When you create any new web application using VS 2010 you will get transform
for web.config file for debug and release environment.
Let’s
see how transformation works. VS by
default do not provide full support for all level of configuration and XML To
transform and preview. There is free tool “SlowCheetah”
available to create and preview of transformation file based on the build
configuration. Click below link to find about slowcheetah.
Follow
the following steps to create and verify transformation:
1. Create
build environment in project
To
create build environment go to visual studio menu option Build ->
Configuration Manager.
Select Active selection configuration dropdown
and click on new. You will get model window to enter build environment name.
There is option copy setting from which show by default debug and release. I
prefer release option. There is also a
check box option “Create new project configuration”. If this is checked build
environment will be create for all project in solution.
Let’s
create development/staging and production build environment.
Make
sure project build environment match with active solution configuration.
2. Create
transformation file in project
VS
by default create web.config and his transform file. Let’s create a new configuration
file MyConfigFile.config. Right click on file and click on “Add Transformation”.
Transformation file will create for each
build environment.
You
will get XML-Document-Transform
namespace in newly created transform file like this
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
</configuration>
Let’s
leave it as it is and go for write transform.
3. Write
transform
The
Transform namespace defines two
attributes: Locator and Transform.
The
Locator attribute specifies the configuration element/elements that you
want to change.
The
Transform attribute specifies what you want to do to the elements which locator attribute finds.
Locator attributes syntax:
Locator="Condition(XPath expression)"
o Find element/elements using specify attribute name.
Locator="Match(comma-delimited list of one or more attribute names)"
o Find element/elements using specify an absolute XPath expression.
Locator="XPath(XPath expression)"
Transform attributes syntax:
·
Replace
the selected element with the element that is specified in the transform file.Transform="Replace"
· Add the element that is defined in the transform file as a sibling to the selected element or elements.
Transform="Insert"
· Insert the element that is defined in the transform XML directly before the element.
Transform="InsertBefore(XPath expression)"
· Insert the element that is defined in the transform XML directly after the element.
Transform="InsertAfter(XPath expression)"
· Removes the selected element.
Transform="Remove"
· Removes the selected element or elements.
Transform="RemoveAll"
· Removes specified attributes from the selected elements.
Transform="RemoveAttributes(comma-delimited list of one or more attribute names)"
· Sets attributes for selected elements to the specified values.
Transform="SetAttributes(comma-delimited list of one or more attribute names)"
Find more detail in this link http://msdn.microsoft.com/en-us/library/dd465326.aspx
Example:
Setting
of MyConfigFile.config file:
<MySettings>
<add name="Payroll"
connectionString="Data Source=development.payroll; Database......."
providerName="System.Data.SqlClient"
/>
<Log4NetTraceListener>
<appender name="Logger" type="log4net.Appender.RollingFileAppender, log4net">
<file value="..\Data\Log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="10MB" />
</appender>
</Log4NetTraceListener>
<location path="c:\MySite\Admin" >
<system.web>
<!--This
is going to remove-->
</system.web>
</location>
</MySettings>
Setting
of my MyConfigFile.config transform file for development build environment:
<MySettings xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<add name="Payroll"
connectionString="Data Source=development.payroll; Database......."
xdt:Locator="Match(name)" xdt:Transform="SetAttributes(connectionString)" />
<Log4NetTraceListener>
<appender xdt:Locator="Condition(@name='Logger')">
<file value="c:\development\Log" xdt:Transform="Replace" />
</appender>
</Log4NetTraceListener>
<location path="c:\MySite\Admin" >
<system.web xdt:Transform="RemoveAll" xdt:Locator="XPath(//system.web)">
</system.web>
</location>
</MySettings>
You
can also preview before actual transformation using tool “SlowCheetah”.
4. Deploy
and generate transformed file using visual studio
Select
build option before publish the site.
Let’s
select development build environment.
Right
click on project file and select publish.
See
what build configuration selected in publish window.
Select
file system build options and choose folder to publish.
Publish
the site.
Go
to the publish folder and open transformed MyConfigFile.config file.
Below
is the transformed file.
<MySettings>
<add name="Payroll"
connectionString="Data Source=development.payroll; Database......."
providerName="System.Data.SqlClient"
/>
<Log4NetTraceListener>
<appender name="Logger" type="log4net.Appender.RollingFileAppender, log4net">
<file value="c:\development\Log"/>
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="10MB" />
</appender>
</Log4NetTraceListener>
<location path="c:\MySite\Admin" >
</location>
</MySettings>
Well
you will find transformation done and you will get changed value in your
configuration file.
Hope
this will help to understand transformation.