in

ASP.NET Weblogs

Drew's Blog

The Joys of Technology Explored

Taking Custom Configuration Sections A Step Further

Something I've found infinitely useful is custom configuration sections By using custom configuration sections, you are allowed to store a richer amount of information in the application config file, than when you simply add values to the <appSettings>section.

To add a custom config section, you must do 2 things: 1) declare the section group and section in the<configSections> portion of the app.config file, and 2) add the new sections to the config file.

[.NET Brain Droppings]

Yup, it's cool, but it gets even cooler when you start writing your own configuration section handler implementations. The beauty of the configuration architecture is that it's completely extensible. To hook into the deserialization process all you need to do is implement a custom IConfigurationSectionHandler. The first time someone requests your named section via ConfigSettings::GetConfig, your implementation of IConfigurationSectionHandler::Create will be called and handed an XmlNode which you can then parse and turn into your own typed configuration object. This just opens infinite possibilities and builds upon the power of XML when it comes to embedding documents.

The prime example of a custom section handler that a lot of people seem to be into lately is the all purpose XmlSerializer based configuration section reader which deserializes sections into .NET types using the power of the XmlSerializer.

The final thing to remember about the configuration system is that it's heirarchical. Enterprise settings, Machine settings and Application specific settings are all unioned together to present a unified configuration to each application when it runs. ASP.NET takes this a step further allowing another steps in the heirarchy according to virtual application directory structure.

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add