Custom configuration section handlers in .NET Thursday, May 08 2008
Configuration files in .NET are amazing just by themselves before you take into account creating your own section handlers. We're creating an application at work that will host multiple instances of itself in the same folders of ASP.NET. This is easy to do, just make sure you know what instance you're using when you execute a page, however it made an annoyance when you have configuration settings that can change per instance and no way to separate the instance configuration settings by separate configuration files. There are no built in configuration section handlers in .NET that can effectively repeat the same config settings but with a top-level key separating them. So what did I do? I created a new configuration section handler for my instance configuration settings.
My end result was that I wanted this
Looking into it, there is a single interface you have to implement, IConfigurationSectionHandler which has only one method:
object Create(object parent, object configContext, XmlNode section)
At the time of requesting your configuration section in code, the Create method of your handler is called. It expects an object in return that represents the data in the configuration file. Just parse the XML node it passes to you, create an object to do lookups on the XmlNode and I had a configuration section handler that handled the config section. Incredibly simple and it makes the configuration file format for my application so much easier to read and understand.
For some examples of a custom config section hadnler, check out GotDotNet's October Feature User Sample winner. They're in no way complex (neither is mine), but it just demonstrates the flexibility and usefulness of .NET configuration files.


I'm not crazy about a lot of things in ASP.Net, but I really love the configuration files, if only for the authentication handling. It's so much less kludgy than doing it the old way in IIS 5.