Sparks from a sparkler

Avoiding the Crash with Config Transformations

Do you drive through an intersection on a daily basis that you know needs a traffic light? There’s an adage that a city won’t put in a traffic light until someone gets hurt. They’re expensive and they can have as much of a negative impact on traffic flow as a positive one. So we keep going through every day hoping nothing bad happens and insisting that something change.

Sometimes it feels like our application environments operate in the same way. We keep rolling through the same intersection just waiting for a crash, but we never see that improvement that we all know we need. Once in a while we’re surprised to find that change we’ve been wanting all along has finally shown up. As of ASP.Net 4.0, a change I’ve been wanting since version 1.1 has arrived. And you wouldn’t believe how much easier it has made my life.

So you build an application that relies on a critical value in your config files but this value changes from one environment to another. I bet you’ve even been desperate enough to edit your machine.config file (I know I have – DON’T DO THAT!). The other option I’ve seen is to keep config files out of source control and edit them in each environment by hand. A slightly safer method has been to keep all values and tie their keys to the domain. An even safer method is to keep as many values as possible in your data. I’ve seen all of these methods used in our projects.

While I still subscribe to the method of keeping as many values in data as you can, once in a while you will still need to manage environment-specific values in your config files.

Welcome our new, super-powerful, ultra-awesome, intricately detailed traffic light: Config Transformations. With config transformations you can specify your values per environment and let the application handle the rest. Take a look at http://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx to learn more about how you can keep just one copy of your web.config (and other config files) and still maintain all of your necessary settings. And no code had to die to get this!