Custom Configuration for .NET (Part 4): Backgrounder on Configuration Sections

Configuration Sections

Updated 2010-06-22: Added link to part 5.


Previous Parts of This Series.

There are two remaining approaches to providing structure that I have not yet covered: collections of elements and section groups. Element collections will be a topic for several future posts as there are a number of different approaches to creating element collections, and two of them make little sense without first covering configuration inheritance (see part 5).

Section groups are much simpler, and mostly a matter of declaring them.

Creating Section Groups

When custom sections are declared using the <configSections> element of a configuration file, one can declare both sections (with the <sections> element, using attributes to define the associated custom type and its name) and section groups.

To define a section group use the—unsurprisingly—<sectionsGroup> element. This needs a name and a type. Often the type will be System.Configuration.ConfigurationSectionGroup which adds no behaviour (but one can customise this, more below) and just allows programmatic access to its child sections and section groups (there is no specified limit to the depth of structure you can define—but don’t go overboard with deep structures which will become increasingly hard to understand and maintain).

For instance:

    <sectionGroup name="userSettings"
          type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=,
                                                                Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
      <section name="recentFiles"
           type="CustomConfiguration.RecentFilesConfigurationSection, MyAssembly"/>

    <recentFiles showFiles="5" removeNoneExistant="true"/>


Which defines a userSettings element to act as a container for (presumably) multiple sections describing many aspects of user behaviour.

Note, replace the strong name for System.Configuration with “System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” for .NET versions 2, 3 and 3.5.

Reading Section Groups and Contained Sections

In previous parts of this series the application code used Configuration.GetSection(‹name›) to get to its custom section (and the instance of Configuration was obtained with ConfigurationManager.OpenExeConfiguration). This works for configuration sections that are immediate children of the root <configuration> node.

To get a section defined with a group simple get the group, and open the section from within that group. For instance, based on the above sample configuration:

var config = ConfigurationManager.OpenExeConfiguration(…);
var userSettingsGroup = config.GetSectionGroup("userSettings");
var recentFiles = (RecentFilesConfigurationSection) userSettingsGroup.GetSection("recentFiles");

GetSectionGroup returns a ConfigurationSectionGroup instance which has GetSection and GetSectionGroup methods as Configuration has.

Configuration Section and Group Collection Properties

You might notice that the Configuration and ConfigurationSectionGroup also have properties Sections and SectionGroups which can be indexed by name. These give another approach to getting a section or section group:

  var myConfig = (MyConfigurationSection)config.GetSection("mine");
  var myConfig2 = (MyConfigurationSection)config.Sections["mine"];

are completely equivalent.

Custom Section Group Types

It is also possible to create your own custom configuration section group by deriving from ConfigurationSectionGroup, and a number of .NET Framework types do this. E.g. System.Xml.Serialization.Configuration.SerializationSectionGroup defined in the System.Xml assembly, which adds properties to type safely access contained sections (i.e. the properties contain the access into the Sections collection and cast the results to the correct type).

Previous Part of This Series on .NET Custom Configuration

  1. Demonstrating Custom Configuration Sections for .NET
  2. Demonstrating Custom Configuration for .NET (Part 2): Custom Elements
  3. Demonstrating Custom Configuration for .NET (Part 3): Validation
This entry was posted Tuesday, May 25th, 2010 16:17. In .NET Core.
You can follow any responses to this entry through the RSS 2.0 feed.

Comments are closed.