Jeffrey Palermo (.com)

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Extending web.config with your custom configuration sections - level 300

The web.config file in ASP.NET enables configuration of the entire ASP.NET application.  It is a file that is part of just about every ASP.NET application, yet I see many developer who roll their own configuration schemes beside the web.config file instead of making use of the functionality already built in.  Yes, some may use the appSettings section and then just retrieve a value from a key, but web.config can do so much more than that!  Here is a sample of how easy it is to add your own configuration section, thereby partitioning a section of configuration items away from the others:

<configuration>

<configSections>
<sectionGroup name="MyCompany.MyApp.MyNamespace">
<section name="SomeClassesConfig"
type=“MyCompany.MyApp.MyConfigHandler“,MyCompany.MyApp" />
</sectionGroup>
</configSections>
<MyCompany.MyApp.MyNamespace>
<SomeClassesConfig>
<add key="myKey" value="myValue" />
</SomeClassesConfig>
</MyCompany.MyApp.MyNamespace>
</configuration>

Then, in your code, you just have a class, MyCompany.MyApp.MyConfigHandler that inherits from System.Configuration.NameValueSectionHandler, and that's all it takes.  If you don't make your own ConfigHandler, then you have to specify the strong name in your type=”” attribute, and not only is that painful, but when you migrate this code to .Net 2.0 and beyond, you will have to modify every place where you gave a strong name.  If you make a superclass of it, then when you migrate the superclass, you're golden.

To get to these values in your code, you just use a NameValueCollection that you can get from calling:

ConfigurationSettings.GetConfig("MyCompany.MyApp.MyNamespace/SomeClassesConfig");

Does that look like an XPath expression to you?

You can further extend the web.config file with your own ConfigHandler that has nested XML elements instead of just a key and a value.  You could store complete object information in the web.config (if the data rarely changes).  Remember that every change of the web.config forces a restart on that directory and its children.

Coming up in May of next year, .Net 2.0 will have the APIs to modify .config files programmatically.  Check out the Beta 1 to use this functionality right now.


Posted 08-22-2004 2:51 PM by Jeffrey Palermo

[Advertisement]

Comments

Noah Coad (MS) wrote re: Extending web.config with your custom configuration sections - level 300
on 08-23-2004 12:29 PM
That is great info Jeff! Did not know about being able to step outside the box like that. I've always used my own "Config" class with "Save" and "Load" methods that serialize/deserializes the class' properties. My big deal with the Configuration namespace's ability is the lack of even notification. Frequently I'll have settings that several modules of code need to track. I use the property/event design pattern so they can subscribe to change notifications. But! This is to change in v2, now one can subscribe to config setting changes (though its not easy).

Cheers!