title: Managing complex Web.Config files between deployment environments
body: <p>Does anyone know of any good tools/utilities for managing <code>Web.Config</code> files between different build/deployment environments?</p>
  
  <p>For example, I have a WCF project that in development I don't want to enable SSL, but I do want it enabled in production.  I want different logging settings, different DB connection strings, different error handling, different file paths... Even some different Unity framework bindings (wire up mocks for unit testing instead of the real objects for deployment).</p>
  
  <p>Maintaining individual copies of the <code>Web.Config</code> is a pain, because adding a new web service means editing multiple files and keeping them in sync.</p>
  
  <p>I've also noticed that if you muck with the <code>Web.Config</code> too much by hand, Visual Studio will choke if you try to use the "add item" wizard to, say, add a new Web Service for WCF, since it has to modify the Web.Config to add the endpoint,a nd can't parse it any more. So I have to be careful not to invalidate the existing Web.Config.</p>
  
  <p>I also thought about just using some regex to do replacements and just building a new <code>Web.Config</code> in a pre-build command. That seems like the best option so far...</p>
  
  <p>Any other ideas? It seems like this should be a very common issue, since the <code>Web.Config</code> should probably never be the same between development and production deployments.</p>
  
  <hr>
  
  <p><strong><em>Update:</em></strong></p>
  
  <p>I decided to write a quick console app that will take all the xml files in a given directory and merge them into one, and only include certain files based on the name.</p>
  
  <p>So I can make in a directory:</p>
  
  <p><strong>WebConfig_All</strong></p>
  
  <pre><code>&lt;configuration&gt;
    &lt;configSections&gt;
      ...
    &lt;/configSections&gt;
    &lt;system.web&gt;
      ...
    &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p><strong>connectionStrings_Debug</strong></p>
  
  <pre><code>&lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="connstr" connectionString="...dev..." /&gt;
    &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p><strong>connectionStrings_Release</strong></p>
  
  <pre><code>&lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="connstr" connectionString="...prod..." /&gt;
    &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Then run my command line tool, and pass in the configuration (Debug, Release, custom...)
  And it will merge all the files that end in <code>_All" or</code>_&lt;configuration&gt;`.</p>
  
  <p>So now I have 80% of my Web.Config in a single <code>WebConfig_All</code> file, and the 20% custom stuff in separate files per build configuration.  I can then run my command line tool as a pre-build task in VisualStudio, or from NAnt, or wherever I want...</p>
  
  <p>I also made my XML merge logic good enough to handle stuff like:</p>
  
  <pre><code>&lt;x&gt;
    &lt;y a="1"&gt;
      &lt;z a="1"/&gt;
    &lt;/y&gt;
  &lt;/x&gt;
  </code></pre>
  
  <p>merge with</p>
  
  <pre><code>&lt;x&gt;
    &lt;y a="1"&gt;
      &lt;z a="2"/&gt;
    &lt;/y&gt;
    &lt;y a="2"/&gt;
  &lt;/x&gt;
  </code></pre>
  
  <p>results in:</p>
  
  <pre><code>&lt;x&gt;
    &lt;y a="1"&gt;
      &lt;z a="1"/&gt;
      &lt;z a="2"/&gt;
    &lt;/y&gt;
    &lt;y a="2"/&gt;
  &lt;/x&gt;
  </code></pre>
  
  <p>Looking good so far... :)</p>
  
  <hr>
  
  <p><em>Followup:</em></p>
  
  <p>This topic is a little old now, so I wanted to point out that VisualStudio 2010 has a feature to do web.config transformations built-in: <a href="http://msdn.microsoft.com/en-us/vstudio/Video/ff801895" rel="noreferrer">http://msdn.microsoft.com/en-us/vstudio/Video/ff801895</a></p>
  
  <p>Of course in typical Microsoft fashion of only implementing any feature 50% of the way, it only works for web projects using web deploy. There is a plugin to enable transformations in other projects, located here: <a href="http://www.hanselman.com/blog/SlowCheetahWebconfigTransformationSyntaxNowGeneralizedForAnyXMLConfigurationFile.aspx" rel="noreferrer">http://www.hanselman.com/blog/SlowCheetahWebconfigTransformationSyntaxNowGeneralizedForAnyXMLConfigurationFile.aspx</a></p>
  
  <p>You could also use a tool like <a href="http://inedo.com/buildmaster/overview" rel="noreferrer">BuildMaster</a> to manage config files (along with builds, tests, DB scripts, etc...)</p>