.NET Config transformations

Creating different configuration files easily

Deniz Ozger
WhoScored Geeks
2 min readJan 14, 2014

--

Just like with most other web applications, we have different configurations for our development, staging and production environments. Moreover, we have different applications for our article page administration, comments, Opta/Enet data processors, news, users etc.

The issue with using a post-build script

We used to run a post-build Ruby script when publishing these applications. It would basically read some hierarchical .yaml files, create a .config file based on .config.template files, and override the Web.config or App.config file. This is one of many ways to manage configurations, but it would generate a modified file on SVN and was a bit prone to human error. We knew there was a space for improvement.

Improving the process with .NET config transformations

.NET provides a much more structured way to generate these config files. You can see the setup if you create a new web application on Visual Studio 2012. There’s also a nice “Preview Transform” option as well, to avoid testing by building the project each time.

We created the same structure on our web applications and it worked like magic. We kept development variables on theWeb.config file, a pretty much blank Web.Debug.config file, and a Web.Release.config file with production data. We also moved all NHibernate configuration settings to Web.config files as well. An example transformation is below, and you can find the not-so-helpful official documentation here.

<system.web>
<caching>
<outputCache enableOutputCache=”true” xdt:Transform=”SetAttributes(enableOutputCache)”></outputCache>
</caching>
</system.web>

Setting this on normal applications that have App.configs instead of Web.configs was slightly trickier though. Instead of using an Add-In like Slow Cheetah, we chose to use a built-in system which is listed as the fourth option in this StackOverflow answer. In order to use the transforms, you need to unload the project by right clicking on its title in the Solution Explorer, edit the .csproj file, and add the code in StackOverflow answer at the end of the file, just before <Project> tag ends.

You will neither get the tree structure for .config files or the Preview Transform option, but when you build the project it will work just like intended.

--

--