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
Wednesday, January 07, 2009 6:12:36 PM UTC
waclyqdb http://qhbvgnpj.com uxdeshrb olnyoypw
Thursday, February 26, 2009 8:15:55 PM UTC
Excellent article! Is there a way to programatically save a collection to the app.config file?
Joel
Thursday, February 26, 2009 8:47:00 PM UTC
Hi Joel,

I am sure you can do that using the System.Configuration.Configuration class and System.Configuration namespace on .NET 2.0 and above

Ivan
Wednesday, July 08, 2009 3:16:25 PM UTC
good post, even two years later. thanks.
Perry
Tuesday, November 24, 2009 3:12:01 AM UTC
Thanks for this example. I searched for hours looking for a solution before I finally found this and it works great.
sirdan
All comments require the approval of the site owner before being displayed.
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

Yeap, the sponsors! :-)

On this page...

Tags

Calendar

<March 2010>
SunMonTueWedThuFriSat
28123456
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