Posted on Friday January 1, 2010

IConfigurationSectionHandler example

This is a small snippet for the basics of writing a ConfigurationHandler to read a configuration section from your web.config or app.config. Since I wrote this I’ve moved to the easier ConfigSection way of doing things, there’s an example here.

You start off with the XML definition in the config file:

And then define your own parser class, and a Settings class to store the details in:

And then when you application first initializes, read the settings like so:

var settings = (Settings)ConfigurationManager.GetSection("mysection");

An alternative way of doing this is to make the Settings class responsible for initializing itself inside a static constructor, using the above line. You would then make the Settings class a singleton.