Archive

Archive for August, 2012

Configuration Transformation in Windows/Console Apps

August 31, 2012 Leave a comment

Introduction

If you are an ASP.NET Web Developer you might have seen the advantages of Web.Config Transformations. Here you can have separate Config files for each configuration defined in “Application Configuration Manager”

image image

But the Configuration Transformations are not supported for Windows Or Console Applications. But there is an work-around way to get it done. Lets see How it is…by Step by Step.

Approach…

image

You can observe that this has only one App.Config file. Lets add two configuration files “App.Test.Config”, “App.Prob.Config”

image

Newly added files are at the Root Level of the Project instead of under App.Config file. See how they are defined in .proj file. (For this you need unload the project and Edit the Project File in Visual Studio)

image

Use DependentUpon  element to make “App.Test.Config”, “App.Prob.Config” files Childs to “App.Config” as  shows below…

image

Reload the Project and See the result

image

Let Add a connection String to the App.Config file.

<?xml version="1.0"?>

<configuration>

    <startup>

        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

    </startup>

 

  <connectionStrings>

    <add name="MyConnString" connectionString="Data Source=.;Initial Catalog=MyDataBase_dev;Integrated Security=True" providerName="System.Data.SqlClient" />

  </connectionStrings>

 

</configuration>

Add Connection strings to Child Config files also… (with the change in database name MyDataBase_Prod, MyDataBase_Test)

<?xml version="1.0" encoding="utf-8" ?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">

 

  <connectionStrings xdt:Transform="Replace">

    <add name="MyConnString" connectionString="Data Source=.;Initial Catalog=MyDataBase_Prod;Integrated Security=True" providerName="System.Data.SqlClient" />

  </connectionStrings>

 

</configuration>

To know more about the Transformation syntax refer below links

http://msdn.microsoft.com/en-us/library/dd465326.aspx

Now…Let Test it by Setting The Project Configuration to Test and Build…

image

If you look at the config file at bin\Test folder…you will find connection string same as “App.config” file.

Why…?

Out of the box, .config file transformation is only available for Web application projects in Visual Studio 2010. Console, WinForms, WPF and other applications projects don’t support this capability out of the box.

What is the Work-Around? TransformXml Task

In this MSBuild code snippet, we are using the TransformXml task defined in the Microsoft.Web.Publishing.Tasks.dll. This is the task responsible for transforming Web.config files in Web application projects. It takes parameters that specify names of the original .config file, the transform file and the output (transformed) file. We use TransformXml task to override AfterCompile target if a transform file exist for the current build configuration. For example, if our project contains a file called App.Debug.config and the current project configuration is Debug, the AfterCompile target will be called. Finally, we use ItemGroup element to change the built-in AppConfigWithTargetPath item collection.

Add Below code to the Project file.

  <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />

  <Target Name="AfterCompile" Condition="exists(‘app.$(Configuration).config’)">

    <TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />

    <ItemGroup>

      <AppConfigWithTargetPath Remove="app.config" />

      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">

        <TargetPath>$(TargetFileName).config</TargetPath>

      </AppConfigWithTargetPath>

    </ItemGroup>

  </Target>

That’s it….!

Build the project in Test Configurations and see the output @ bin\Test folder…!

Hope this Helps..Smile

Advertisements
Categories: Uncategorized