Configuration
  Write Client Configuration
  Retrieve Configuration
  Custom Config Handler

Get URL for this page

How Do I...Write a Custom Configuration Section Handler?

When configurations become too complex for the simple section handlers provided by the System.Configuration namespace, a custom section handler should be created. All that is needed for a custom section handler is for a class to support the IConfigurationSectionHandler interface. A basic class definition for the handler might look like the following.


Public Class MyConfigurationSectionHandler : Implements IConfigurationSectionHandler
 ..
End Class
VB

The IConfigurationSectionHandler interface has only one method. The Create method is called on the section handler whenever a configuration section is found that is registered to the handler. The Create method accepts three parameters. The first parameter is an Object that contains parent configuration settings in the instance that inherited configuration settings exist. The second parameter is a ConfigurationContext that contains any contextual information such as the current virtual path. This information is provided for convenience, and doesn't have to be used. The final parameter is an XmlNode containing the raw XML configuration. The DictionarySectionHandler returns a hash table, but a handler can return any possible type after parsing data. A sample implementation of Create might consist of the following code sample.


Public Function Create(Parent As Object, ConfigContext As ConfigurationContext, Section As XmlNode) As Object
    Dim Hash As New Hashtable()
    ...
    Return Hash
End Class
VB

When trying to manipulate and investigate the parent properties, keep the following guidelines in mind: the parent object should never be modified in the Create method; any modifications, no matter how small, should be made on a clone; the parent object may also be null; if the section handler is being called for the first time, there won't be any inheritable settings available. The following code example demonstrates proper use of the parent object.


Dim Hash As Hashtable

If Parent = Nothing
    Hash = New Hashtable()
Else
    Hash = CType(CType(parent, Hashtable).Clone(), Hashtable)
End If
VB


Copyright 2001 Microsoft Corporation. All rights reserved.