Storing a collection on your app.config using Section Handlers

Posted in app.config | Section Handlers at Tuesday, October 16, 2007 6:22 PM UTC
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;
}


kick it on DotNetKicks.com AddThis Social Bookmark Button

Monday, February 18, 2008 10:39:07 AM UTC
thank you, thank you, thank you!!
Liezl
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, em, i, strike, strong) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview

On this page...

Tags

Calendar

<October 2007>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Navigation

Archives

Recommended .NET blogs

The author

Iván Loire
Iván Loire (Zaragoza, Spain)

View Ivan Loire's profile on LinkedIn
Microsoft Certified Trainer
Send mail to the author(s) E-mail

Syndication

Feed Icon