Updating Config Files in ASP.NET 2.0

One of the new features of ASP.NET 2.0 is the ability to not only more easily read from config files, but also to update them. If you’re looking for a pretty good description of what’s possible, check out the ASP.NET QuickStart Tutorial. Going over the basic process isn’t the point of this post. Instead, I want to talk about an unexpected, but yet expected, side effect.

The situation is quite common. Your web site utilizes Session variables. Over the course of your application, you update one of the config files that you had defined. All is fine and wonderful right up to the point where a Session variable was accessed. For some unknown reason, it was gone.

If this particular situation sounds familiar, there could very well be a good reason. It is similar to what happens if web.config gets updated on an active virtual directory. At least the symptoms are. What is actually happening is that the web application gets restarted. So it should be a surprise, should it.

Well, no. With one minor exception. In the particular case we were looking at, the config file was actually defined as an external file. So in web.config there was an entry similar to:

<customAppSettings configSource=”customAppSettings.config” />

So the actual updates were taking place outside of the web.config file. Since web.config isn’t updated, the application shouldn’t restart. Should it?

Well it turns out that the default behavior of the Config API is that changes to the external file cause an application restart just like web.config. And with the application restart comes the loss of Session information

There is a way to modify this default behavior. In the <section> tag where the config section is defined, the restartOnExternalChanges attribute gets set to false.

<section name="customAppSettings" type="ObjectSharp.Demo.CustomAppSettingsSection, ObjectSharp.Demo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a140608022e61b4b" restartOnExternalChanges="false" />

All of a sudden updating the config file doesn’t cause an application restart and all of the little session variables remain fat and happy.