Why I hate web.config
Posted at 12:35 on 30 April 2008
One thing that is vital when deploying web applications is that you should be able to reduce the process of deploying upgrades and changes to as few steps as possible. Furthermore, every step should be a no-brainer -- so simple that the scope for fat fingering something is strictly limited.
This kind of thing is acceptable:
- Get the appropriate stable build from your daily build server.
- FTP it onto the web server into a directory in an appropriate location. (Even better: have an option in your build script to do this automatically.)
- Change the IIS settings to point to the new version.
- You're done!
Now in order to do this effectively, you need to build some foundations into your project. You need to isolate every setting that varies between your production environment and your developer box and put them in a separate location outside the website's hierarchy that does not change from build to build.
These settings are purely concerned with server-specific configuration settings. They change from one machine to the next and will be different between developer machines and the production server. Examples include connection strings, SMTP server details, custom errors and trace settings. They aren't necessarily stored in your source control, except as a sample file for documentation purposes, and they should definitely not be deployed afresh to the server with every build.
There are other settings that are tied much more closely to the code itself. Examples include HTTP handlers and modules, assemblies referenced in the <compilation>
section, and all the additional stuff that ASP.NET Ajax or ASP.NET 3.5 adds to tell it that you're using the C# 3.0 compiler, not the C# 2.0 compiler. These settings may change from one build to the next, but they are the same on every machine where they are used. They are, to all intents and purposes, code, and should be treated as such, kept in your source control, and deployed unmodified to the server with every new build.
Unfortunately, web.config mixes the two willy-nilly in a thoroughly cavalier way, with the result that there are several additional, more complex and error-prone steps that you need to take:
- Locate the previous build.
- Copy the web.config file into the new build.
- Merge in the changes manually.
These steps are less straightforward and provide much more scope for error. What if you forget to do them, make a dog's dinner of merging in the changes, or worse, introduce some subtle and mysterious bug that isn't there on your development machine?
ASP.NET 2.0 added a new feature to sort this mess out. You can now specify an alternative file for your <appSettings>
section. By doing <appSettings file="..\myappsettings.config" />
you can even specify a file outside your web application root. Whoopee! Problem solved!
Not so fast. What about the settings that don't fit in to <appSettings>
? For example, connection strings now go in the <connectionStrings>
section; custom errors should be enabled on the server but disabled on your development box; tracing should be enabled on your development machine but not on the server; and so on.
It turns out that these too have an option to allow you to reference external files. You can set, say, <connectionStrings configSource="blah" />
to put your connection strings in a separate file. Unfortunately, unlike with <appSettings>
, you can't put this outside your application root.
Meh. Why not??? This is a major pain in the neck -- especially for <connectionStrings>
.
To make matters worse, there are some elements that straddle both camps. is the most obvious example. It needs to have the attribute
debug="true"
on a development server, but in production you will need to insert debug="false"
for improved performance. However, within your element, you have a list of additional assembly references for things such as the ASP.NET Ajax extensions. And you can't put these in a separate file.
All in all, configSource
and go some of the way towards solving the deployment problem. Unfortunately, they still have limitations that are awkward and hobble the process and are a major annoyance.