DCSIMG
How to add a custom configuration section - Wortzel's blog

Wortzel's blog

.Net (2.0, 3.0, 3.5), C#, Asp.net, Com+, GIS(ESRI Software), Management, Analysis & Design, Life, Trips, And more...
How to add a custom configuration section

It's a best practice to work with strong types instead of typing the name of the class/properties by our own hands. One of the common places that we are usually typing a string is when we're retrieving data from configuration file. In this post I will show you how to declare a custom configuration section with a strongly typed class.

1.     Declare the configuration section in configuration file:
<configSections>
   <section name="serviceSettings" type="ServiceSettingsHandler"></section>
</configSections>
2.     Add the configuration section with its data:
<serviceSettings>   
   
<
service Assembly="asm1" Class="class1"/>   
   
<
service Assembly="asm2" Class="class2"/>   
   
<
service Assembly="asm3" Class="class3"/>
</serviceSettings>
3.     Add a new class that will represent the service definition (assembly & class name):
public class ServiceSetting
{   
   
public ServiceSetting(string className, string assembly)
   
   
{       
       
_Class = className;
       
       
_Assembly = assembly;
   
   
}
   
   
private string _Class;
   
   
public string Class
   
   
{
       
       
get { return _Class; }
   
   
}
   
   
private string _Assembly;
   
   
public string Assembly
   
   
{
        
       
get { return _Assembly; }
   
   
}
}
4.     Add a handler class. This class must implement the IConfigurationSectionHandler interface. This interface includes only one method that calls "Create", which creates a configuration section handler. The handler class look like this:
public class ServiceSettingsHandler : IConfigurationSectionHandler
{   
   
#region
IConfigurationSectionHandler Members   
   
public object Create(object parent, object configContext,
       
System.Xml.XmlNode section)   
   
{
       
       
List<ServiceSetting> list = new List<ServiceSetting>();
        
       
foreach (XmlNode service in section.ChildNodes)
       
       
{
            
            
list.Add(new ServiceSetting(
service.Attributes     
                ["Class"].Value ,
 service.Attributes
                ["Assembly"].Value));
       
        }
        
       
return list ;
   
    
}
   
    
#endregion
}
5.     The last thing we should do is to create a wrapper class to work with strongly types. The class should looks like this:
public class ServiceConfigurationManager
{   
    private ServiceConfigurationManager()
    { }    
   
public static List<ServiceSetting> GetServices()
   
   
{
       
       
return (List<ServiceSetting>)ConfigurationManager.
           
GetSection("serviceSettings");   
    }
} 
And now let's write some code that will use this custom configuration handler:
protected void GetCustomConfigSectionDate_Click(object sender, EventArgs e)  
{
   
List<ServiceSetting> list =
       
ServiceConfigurationManager.GetServices();      
    Services.DataSource = list;
     
   
Services.DataBind();
}
And the result is:

Published Tuesday, October 23, 2007 8:34 AM by Avi Wortzel

Comments

# re: How to add a custom configuration section @ Tuesday, October 23, 2007 11:50 AM

Good post!

Maor David-Pur

# re: How to add a custom configuration section @ Thursday, November 01, 2007 8:18 AM

Thanks... :)

Avi Wortzel

# re: How to add a custom configuration section @ Thursday, November 22, 2007 8:31 AM

Avi,

Another further step you can do is to fully use XML serializable objects, and by that avoid the XMLDom parsing. Suppose your ServiceSettings is decorated with some XML Serialization attributes (XmlRoot, XmlElement), you can then use it as follows:

public object Create(object parent, object configContext, XmlNode section)

       {

           ServiceSettings settings = new ServiceSettings ();

           if (section != null)

           {

               settings = SettingsDataSeralizer.Deserialize(section.OuterXml);

           }

           return settings;

       }

Of course, you need a ServiceSettingsCollection above it that will store the ServiceSettings array (actually should be Service[]), which then can be populated easily the same with data bind.

Nir N

# Host multiple services in windows service (on-line WCF services and batch services)@ Friday, December 14, 2007 3:45 PM

In the last sprint we ( Ori – my teammate and me) had a task to create an infrastructure to manage some

Wortzel's blog

# re: How to add a custom configuration section @ Wednesday, June 18, 2008 11:47 AM

Was what it took med to read List<xxx> from app.config by following your recipt ! Thanks.

C. Bonde

# re: How to add a custom configuration section @ Tuesday, February 24, 2009 12:55 AM

Greate  post .... Thanks  

Bilal