Are you cheeting yet?
Published on Tuesday, October 22, 2013 3:20:00 PM UTC in Programming & Tools
Visual Studio supports transformations for web.config files that allow you to to change any details of your configuration during the deployment process, depending on your build configuration. This comes in handy to e.g. change application settings or connection strings for a release build, because typically those values will be different from the ones you are using during development or for test runs. One limitation of this feature is that it's only available for web.config files and cannot be applied to the configurations of client applications. Luckily, a nice solution to this exists.
When you create a new web application, let's say an ASP.NET MVC project, some transformation files are added to the project's configuration for you automatically:
By default, these transforms make sure that some debug settings are removed in release builds, for example.
The possibilities go far beyond simply removing attributes though. In particular, you can do things like:
- Work with conditions and pattern matching to locate elements
- Replace, insert, remove or change elements or their attributes, respectively
If you want to learn more about this technique in general, this is a good quickstart tutorial, and the MSDN documentation describes all transform options in more detail.
The thing with client applications is that they use an app.config file instead of a web.config, and despite the similar concept and configuration mechanism, there's no built-in feature to perform the same transformations for these. Now what?
Luckily there's a project maintained by Sayed Hashimi named "Slow Cheetah". I leave it up to your imagination whether this was inspired by a song from Red Hot Chili Peppers or something different (I honestly don't know), but fact is: despite the somewhat odd name, this is an excellent tool to add similiar transformation capabilities to app.config or other XML files.
The simplest way to get access to the project is to use the "Extensions and Updates" manager in Visual Studio in the "Tools" menu (search for "SlowCheetah"). After a restart of Visual Studio, you will find a new context menu entry e.g. for app.config files that allows you to add transform files.
There you go, now you can use the same transformations during build time for your app.config files too. This really comes in handy, and I'm regularly using it for most of my non-web projects. It supports even setup and ClickOnce deployment scenarios and has additional features e.g. to preview transformed files in a diff view. More information can be found in the Visual Studio gallery, and the source code is available on GitHub.
More Separation of Concerns
Here's an additional technique that I use to further separate somewhat static configuration settings from the ones that are truly dynamic and may change depending on the deployment target. For example, things like the database provider factories, the target framework version or assembly bindings are typically not something that change frequently, but the corresponding connection strings to databases, or other settings like SMTP servers and custom app settings may heavily depend on the deployment scenario.
To achieve a bit more separation for these details, you can work with the little known attribute named "configSource" that let's you point to a separate file that contains the actual configuration values:
The target file then contains the desired element as sole root node (in this case "appSettings"), with all the sub elements that you would otherwise add to the element in the main config file directly. Of course this can be combined with the above mentioned transforms. Simply use a transformation file to let the "configSource" attribute point to a different file instead:
The "configSource" attribute is a standard mechanism of config files, but in conjunction with SlowCheetah it allows a consistent and nice separation of concerns for both web and non-web projects.
Tags: .NET · ASP.NET · SlowCheetha