Storing a collection on your app.config using Section Handlers

Posted in app.config | Section Handlers at Tuesday, October 16, 2007 7:22 PM GMT Daylight Time
If you followed this article, you learned how to implement the IConfigurationSectionHandler interface in a custom class to store and read data on or from the app.config (or web.config) using your own typed data structure.

It is also common that you need to store a collection of values on that app.config. Example (see ProxyConfiguracionProcesos tag):


(note that the above image has been cropped to fit the space)

If that's the case, you can create a class to store a single item (in this case a "Proceso"):
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace PIENSA.UI.Service.ComunicacionAutomata.ConfigurationHandler
{
struct ProxyConfigurationItem
{
public string Name;
public string AssemblyLocation;
public string ClassName;
public int ExecutionInterval;
public byte Priority;
public byte LogLevel;
public int AlarmIfTakesMore;
public int AlertIfTakesMore;
}

and then parse every item on the custom section using your SectionHandler class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;

namespace PIENSA.UI.Service.ComunicacionAutomata.ConfigurationHandler
{
class ProxyConfigurationHandler : IConfigurationSectionHandler
{
public ProxyConfigurationHandler() { }



public object Create(object parent,
object configContext, System.Xml.XmlNode section)
{
List<ProxyConfigurationItem> items = new List<ProxyConfigurationItem>();
System.Xml.XmlNodeList processesNodes= section.SelectNodes("Proceso");

//process each Node "Proceso" foreach (XmlNode processNode in processesNodes){
ProxyConfigurationItem item = new ProxyConfigurationItem();
item.Name = processNode.Attributes["Name"].InnerText;
item.AssemblyLocation = processNode.Attributes["Assembly"].InnerText;
item.ClassName = processNode.Attributes["Clase"].InnerText;
item.ExecutionInterval = Convert.ToInt16(processNode.Attributes["Interval"].InnerText);
item.Priority = Convert.ToByte(processNode.Attributes["Prioridad"].InnerText);
item.LogLevel = Convert.ToByte(processNode.Attributes["LogLevel"].InnerText);
item.AlertIfTakesMore = Convert.ToInt32(processNode.Attributes["AlertIfTakesMore"].InnerText);
item.AlarmIfTakesMore = Convert.ToInt32(processNode.Attributes["AlarmIfTakesMore"].InnerText);
items.Add(item);
}
return items;
}

}
}
Also note that you will need a section handler declaration like this:
<section name="ProxyConfiguracionProcesos" type="PIENSA.UI.Service.ComunicacionAutomata.ConfigurationHandler.ProxyConfigurationHandler, PIENSA.UI.Service.ComunicacionAutomata" />
where the first part in the type attribute is the custom class that implements the IConfigurationSectionHandler interface and the second is the assembly where that class is located.

Now, from any part of the code, you can get your collection of processes, as defined on the app.config:

  private List<PIENSA.UI.Service.ComunicacionAutomata.ConfigurationHandler.ProxyConfigurationItem> GetProcessesConfiguration() {
List<PIENSA.UI.Service.ComunicacionAutomata.ConfigurationHandler.ProxyConfigurationItem> processesConfig= (List<PIENSA.UI.Service.ComunicacionAutomata.ConfigurationHandler.ProxyConfigurationItem>)System.Configuration.ConfigurationSettings.GetConfig("ProxyConfiguracionProcesos");
return
processesConfig;
}


AddThis Social Bookmark Button