Introduction

In the
part II article we demonstrate how to create a custom section in the configuration file using the configuration section. In this part III article we will try to achieve, step by step, the same goal as in the Part II article, but using another tool, namely, the IConfigurationSectionHandler interface. Now, let us resume the situation treated in the previous article, I mean, we assume that there is an information system composed by a number of applications as a part of an enterprise planning resource ERP. The application F, used by the financial department, listens, via a given port, to the application A witch is used by the Account department. We want enable changing listen ports values outside the design time, exactly, when the application is already deployed. I gave solutions in the previous articles to do that using the ConfigurationSection class, but there is another way to achieve the same purpose, and this once, using the IConfigurationSectionHandler interface. It is more difficult than the previous solution because it needs little advanced programming skills. In general, this interface is used to access some sections more than to create them. But in this article I try to use this interface for a different purpose, I mean, I implement it in a new configuration section class in order to create an entire section profiting of its Create ( ) method. Of Course, it remains my strictly personal way to use the IConfigurationSectionHandler, but there are surely other ideas at least better than mine to deal with this interface. Before beginning, I have two suggestions. The first one is that the lector is strongly invited to read the previous articles in order to understand as well as possible the problem treated by this article and the resolution process proposed to deal with. The second one is that the lector has to have a minimum idea about what's the DOM (Document object model) and how to deal with this technology within a C# programming context. Now, let's begin.

Presentation of the IConfigurationSectionHandler Interface

The IConfigurationSectionHandler interface is an interface that handles the access to a certain sections; in addition to that, it is possible to use it in order to create new sections in the application configuration files.



Figure 1

As the figure 1 shows, this interface contains a non implanted method witch is Create ( ), this method is overloaded by three arguments, the first one is supposed to represent the parent witch is the targeted configuration file. The second one is supposed to represent the configuration context object and the third one is intended to represent the section and it has an XmlNode as type. Generally, this method is used in a multithread context, I mean; the method is assumed to be callable from more than one thread simultaneously, but we assume, to do not take in consideration this aspect in order to simplify the situation already complicated. The IConfigurationSectionHandler interface is represented bellow:

Figure 2

Now, this is the representation of the class that I proposed to create a section in the configuration file



Figure 3

The configuration section class building steps

Now, let us explain the mechanism:

Preliminary steps

namespace PortsSectionHandler
{
public class PortSectionIHandler :
IConfigurationSectionHandler
{
}
}

We consider, as in the previous article, that a port section has some attributes witch are:

Class members presentation

Properties

Name Type Visibility Description
KeyValue String Public Represents the key value witch is a string, it is used to identify a given port
SerialValue String Public Represents the serial value witch is a number but represented with string format, it is used after its conversion to integer to set the key serial or number
SectionName String Public Represents the section name

Constructors

I propose three constructors for use:

Methods

Exception

The KeyDuplicationException is especially derived from the Exception class to help avoid the situation when a key value is duplicated.

To use this class add, implement and call this followed method from your code:

private static void AddNewSection()

{

// Create a new PortSectionIHandler object

PortSectionIHandler CreateSection = new PortSectionIHandler("port", "key8", "9200");

// Create parent object witch is a configuration object in this context

object Parent = new object();

// Create configContext object witch indicates the targeted configuration file

object configContext = new object();

// Create the XmlNode witch indicates the section

section = CreateSection.SetXmlNode();

// Apply the Create () method provided by the IConfigurationSectionHandler interface

bool Created = (Boolean)CreateSection.Create(Parent, configContext, section);

// Profiting of the returned object to indicates that section has been created

if (Created == true) MessageBox.Show("The section has been created successfuly");
}