app.config Transformations

isNaN1247 picture isNaN1247 · May 31, 2011 · Viewed 23.8k times · Source

I'm a huge fan of the addition of web.config transformations in Visual Studio 2010. See also Scott Hanselman's recent talk at MIX2011.

What sucks is that this functionality (appears at least) to only be available to web projects.

In our solution we have several Windows Services that connect to a different database dependant on the environment they are deployed under.

Has anyone come up with a nice, tidy way of achieving similar 'app.config transformation' functionality?

Note: We are using TFS 2010 to build our solutions in a Continuous Integration manner.

Answer

Luke Bennett picture Luke Bennett · May 31, 2011

You can use the XML transformation functionality with any XML file - we do this all the time. It's available via an MSBuild task.

Try adding the following to your build script:

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

<TransformXml Source="Path\To\Your\Xml.config"
              Transform="Path\To\Your\Xml.$(Configuration).config"
              Destination="Path\To\Your\Output.config" />