Geeks With Blogs

News This is the *old* blog. The new one is at blog.sixeyed.com
Elton Stoneman
This is the *old* blog. The new one is at blog.sixeyed.com

[Source: http://geekswithblogs.net/EltonStoneman]

Web.config transformation is a simple and powerful inclusion in .NET 4.0 for generating configuration files for different environments. It's a templated match-and replace, and you can put together a homegrown alternative with T4 and some scripting, but the integrated experience is better. It's limited to web.config in Visual Studio, but with a simple MSBuild target you can leverage it for any config file.

The transform is based on your VS.NET solution configurations, so you can start by replacing the standard "Debug" and "Release" builds with custom configurations to suit your environment. Then run "Add config transformations" by right-clicking the web.config file and VS will generate stub files for each type of build:

The original Web.config file is the source file, and the .DEV, .TEST and .PROD versions are the transform files which contain the overrides needed for each environment. So your Web.config may define a database connection:

  <connectionStrings>

    <add name="db1"

         connectionString=" "

         providerName="System.Data.SqlClient" />

  </connectionStrings>

- and in Web.DEV.config you provide the connection string for the development environment and specify a matching pattern. In this case specify to match on the name attribute of the element, and all the other attributes you provide will replace those in the source (there are more complex transformations available):

  <connectionStrings>

    <add name="db1"

      connectionString="Data Source=.\SQLEXPRESS;Initiali Catalog=xyz;Integrated Security=SSPI;"

      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

  </connectionStrings>

Switch to the DEV build type, right-click the web project and select "Build Deployment Package" to generate a config file for dev which contains the transformed node with the connectionString attribute from the transform file and the providerName attribute from the source file:

  <connectionStrings>

    <add name="db1"

         connectionString="Data Source=.\SQLEXPRESS;Initiali Catalog=xyz;Integrated Security=SSPI;"

         providerName="System.Data.SqlClient" />

  </connectionStrings>

All straightforward, and the deployment package VS builds is used with the WebDeploy tool, and makes it all very simple. The actual config file will be created in obj\<Configuration>\TransformWebConfig\transformed. You can get the same result from MSBuild by building the solution file with the property DeployOnBuild=True.

The actual transform logic is all in the TransformXml MSBuild task from Microsoft.Web.Publishing.Tasks.dll. So you can utilise the same functionality for generating any config file with a simple MSBuild script:

  <UsingTask TaskName="TransformXml" AssemblyFile="bin\Microsoft.Web.Publishing.Tasks.dll"/>

 

  <Target Name="GenerateConfigs">

    <MakeDir Directories="$(BuildOutput)" Condition="!Exists('$(BuildOutput)')"/>

    <TransformXml Source="BTSNTSvc.exe.config"

                  Transform="BTSNTSvc.exe.$(Configuration).config"

                  Destination="$(BuildOutput)\BTSNTSvc.exe.config"/>

  </Target>

You'll have to manually create the source and transform files for each environment, but you can maintain the naming convention so the pattern is consistent across your Web projects and other config targets. One thing the transform doesn't do is render the runtime value of MSBuild properties – so if you want to include the build version number in an attribute of the config file, then you'll need a custom step before calling TransformXml to parse the property values.

For application config files you can have this step in your build prior to creating MSIs, so that the MSI has the correct config values when deployed. For external configs – if you need to populate say machine.config or BTSNTSvc.exe.config – you can create the configs in the build and have a manual deploy step to overwrite the files, or if you're brave you could create an MSI which just contains the config files and overwrites the targets on install.

Note that Microsoft.Web.Publishing.Tasks.dll is part of VS 2010 and not part of MSBuild (default install location: C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\Web), so you will need to be licenced to use the assembly on your build server.

This is an excellent way to centralise all your environment variables in source control along with the project code, and removes the risk of manual config updates as part of deployment.

Posted on Friday, August 20, 2010 1:06 PM MSBuild , .NET 4.0 | Back to top


Comments on this post: Using MSBuild 4.0 web.config Transformation to Generate Any Config File

# re: Using MSBuild 4.0 web.config Transformation to Generate Any Config File
Requesting Gravatar...
Thanks to all employees
Left by desenhane on Sep 13, 2010 3:53 PM

# re: Using MSBuild 4.0 web.config Transformation to Generate Any Config File
Requesting Gravatar...
I think I can do this coding by myself. I will set it up at the office. revitol stretch mark cream reviews
Left by Andrew on Aug 22, 2011 12:44 PM

# re: Using MSBuild 4.0 web.config Transformation to Generate Any Config File
Requesting Gravatar...
Thanks!
Here is a working mini sample. I needed to explicitly target Msbuild4.0 to get this to work. (I have multiple versions of VS installed)

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TransformXml" AssemblyFile="C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.Dll"/>

<Target Name="GenerateConfigs">
<Message Text="Transfom Config." />
<TransformXml Source="web.config" Transform="web.Test.Config" Destination="web2.config" />
<Message Text="Done Transfom Config." />
</Target>
</Project>

To execute:
"c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild" buildsample.xml

Thanks!
Drew
Left by drewbai on Oct 15, 2011 11:30 PM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net