Custom Configuration Section Handler in .NET 2.0

 

I recently had to deal with creating custom sections in the web/app.config file. In the .NET 1.x realm, I had to create a section handler which implements public class CustomSectionHandler: IConfigurationSectionHandler.

In 2.0, we have to create a custom section handler that derives from ConfigurationSection, which then can be used to contain public properties that map the attributes in the configuration file.

For example, Let's say that we have the following custom section in web.config:

   MySection Attribute1="1" Attribute2="x" />

We can write a custom section like the following:

using System.Configuration;

public class MyConfigSection: ConfigurationSection

{

   [ConfigurationProperty("Attribute1", IsKey = true, IsRequired = true)]

public int Attribute1

   {

      get { return (int)this["Attribute1"]; }

      set { this["Attribute1"] = value; }

   }

   [ConfigurationProperty("Attribute2")]

   public string Attribute2

   {

      get { return (string)this["Attribute2"]; }

      set { this["Attribute2"] = value; }

   }

}

If you want to support mutiple elements within a config section, you can do so by creating another class that derives from ConfigurationElementCollection to maintain a collection of the configuration element. We can tweak the custom section in the web.config file a little bit to support this.

MyConfigSection>

Employees>

Employee EmployeeID="alincoln" FirstName="abraham" LastName="lincoln" />

Employee EmployeeID="gwashington" FirstName="george" LastName="washington" />

Employee EmployeeID="gbush" FirstName="george" LastName="bush" />

Employees>

MyConfigSection>

Then, we can write the custon section like this:

public class MyConfigSection: ConfigurationSection

{

   [ConfigurationProperty("Employees", IsDefaultCollection = true)]

   public EmployeeCollection Employees

   {

      get { return (EmployeeCollection) base["Employees"]; }

   }

}

public sealed class EmployeeElement : ConfigurationElement

{

   [ConfigurationProperty("EmployeeID", IsKey = true, IsRequired = true)]

   public string EmployeeID

   {

      get { return (string)this["EmployeeID"]; }

      set { this["EmployeeID"] = value; }

   }

   [ConfigurationProperty("FirstName", IsRequired = true)]

   public string FirstName

   {

      get { return (string)this["FirstName"]; }

      set { this["FirstName"] = value; }

   }

   [ConfigurationProperty("LastName", IsRequired = true)]

   public string LastName

   {

      get { return (string)this["LastName"]; }

      set { this["LastName"] = value; }

   }

}

public sealed class EmployeeCollection : ConfigurationElementCollection

{

   protected override ConfigurationElement CreateNewElement()

   {

      return new EmployeeElement();

   }

   protected override object GetElementKey(ConfigurationElement element)

   {

      return ((EmployeeElement)element).EmployeeID;

   }

   public override ConfigurationElementCollectionType CollectionType

   {

      get { return ConfigurationElementCollectionType.BasicMap; }

   }

   protected override string ElementName

   {

      get { return "EmployeeElement"; }

   }

   public EmployeeElement this[int index]

   {

      get   {      return (EmployeeElement)BaseGet(index);   }

      set   

      {

            if (BaseGet(index) != null)

         {

               BaseRemoveAt(index);

         }

         BaseAdd(index, value);

      }

   }

   new public EmployeeElement this[string employeeID]

   {

      get{return (EmployeeElement)BaseGet(employeeID);}

   }

   public bool ContainsKey(string key)

   {

         bool result = false;

         object[] keys = BaseGetAllKeys();

         foreach (object obj in keys)

         {

               if ((string)obj == key)

               {

                  result = true;

                  break;

               }

         }

         return result;

   }

}

I've added the ContainsKey() Method to the EmployeeCollection class because I couldn't find a simpler way to check for the existence of a given key. Maybe there is a better solution..

Now you need to add the custom section to the ConfigSections in the coniguration file.

section name="MyConfigSection" type="Namespace.ClassName, AssemblyName" />

Lastly, you can access the config section from your code the following way:

MyConfigSection employeeSection=

(MyConfigSection )System.Configuration.ConfigurationManager.GetSection("MyConfigSection");

I hope it helps!

Published Tuesday, August 08, 2006 8:18 AM by Jason Jung

原文:http://blogs.neudesic.com/blogs/jason_jung/archive/2006/08/08/208.aspx

你可能感兴趣的:(configuration)