Creating a custom Configuration Section in .NET

Every now and then I find myself needing to create a custom configuration section for a .NET application or module that I’m building. It’s not something I do often enough to remember how to do it by heart though so I usually consult Google, only there I often find guides that are either to long and/or verbose or doesn’t quite match how I like to do it. So I end up digging up some of my old code where I’ve created a custom configuration section. While that works it takes more time than it should so I thought I’d help myself by posting a quick little guide here.

Creating a basic Configuration Section

Given the class below…

using System.Configuration;

namespace ConfigurableModule
{
    public class Configuration : ConfigurationSection  
    {
        public static Configuration GetConfiguration()
        {
            Configuration configuration = 
                ConfigurationManager
                .GetSection("myConfigurableModule")
                as Configuration;

            if (configuration != null)
                return configuration;

            return new Configuration();
        }

        [ConfigurationProperty("message", IsRequired = false)]
        public string Message
        {
            get
            {
                return this["message"] as string;
            }
        }
    }
}

…and the following in our applications configuration file (app.config/web.config)…

<configuration>
    <configSections>
        <section name="myConfigurableModule" type="ConfigurableModule.Configuration, ConfigurableModule"/>
    </configSections>
    <myConfigurableModule message="Hello world!" />
    <!-- Optionally other configuration -->
</configuration>

…we can get the Message property from the configuration like this:

string messageFromConfiguration
    = ConfigurableModule.Configuration
        .GetConfiguration().Message ;

Storing the configuration section in a separate file

We can move our configuration section to a separate file using the configSource attribute. To do that in the above example we change it like this:

<configuration>
    <configSections>
        <section name="myConfigurableModule" type="ConfigurableModule.Configuration, ConfigurableModule"/>
    </configSections>
    <myConfigurableModule configSource="myConfigurableModule.config" />
    <!-- Optionally other configuration -->
</configuration>

The file myConfigurableModule.config should then look like this:

<myConfigurableModule 
    message="Hello world from a separate file!" />

Creating ConfigurationElements, ConfigurationElementCollections etc

The above is usually all I need, but if a more extensive configuration is needed, requiring separate elements etc there is a more extensive guide on MSDN.

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Comments

  1. Daniel marbach's avatar

    Daniel marbach 1 years ago

    Hy
    See http://www.planetgeek.ch/2009/02/23/configuration-sections-the-easy-way/

    Greets daniel

  2. Paul's avatar

    Paul 1 years ago

    Hi Joel,
    try the Configuration Section Designer over on codeplex, I've found it to be a great help.

    http://csd.codeplex.com/

    You can then encrypt the custom sections too, although if you have a collection type, you may need to add the following to the collection class. I'm not sure if this was fixed in the current version.

    protected override string ElementName

    I can provide an example if you need one.

Follow me on Twitter

  1. @tim_abell The gigantic ones are for customers who specifically ask for them only :) 1 months ago
  2. Looking to buy a gigantic easter egg filled with candy for delivery in Stockholm. Any recommendations? 1 months ago
  3. @strandberg_m Du måste skriva om resultatet efteråt! 1 months ago
follow me

Latest comments

  1. Joel Abrahamsson wrote "Hi Jonas! The fluent API is really geared towards working..." on Building a search page for an EPiServer site using Truffler
  2. Jonas wrote "Thank you for one more great write up! If you're not lucky ..." on Building a search page for an EPiServer site using Truffler
  3. David Knipe wrote "The CategoriesFacet method will save me a load of headaches ..." on Cool new features in the Truffler .NET API

About this site

This blog is built with EPiServer Community, EPiServer CMS, ASP.NET MVC and a bunch of other great products. The source code is available for download at the projects page, where you also can read more about this site and my other projects.

read more