Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement a ConfigurationSection with a ConfigurationElementCollection
    text
    copied!<p>I am trying to implement a custom configuration section in a project and I keep running up against exceptions that I do not understand. I am hoping someone can fill in the blanks here. </p> <p>I have <code>App.config</code> that looks like this: </p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt; &lt;configSections&gt; &lt;section name="ServicesSection" type="RT.Core.Config.ServicesConfigurationSectionHandler, RT.Core"/&gt; &lt;/configSections&gt; &lt;ServicesSection type="RT.Core.Config.ServicesSection, RT.Core"&gt; &lt;Services&gt; &lt;AddService Port="6996" ReportType="File" /&gt; &lt;AddService Port="7001" ReportType="Other" /&gt; &lt;/Services&gt; &lt;/ServicesSection&gt; &lt;/configuration&gt; </code></pre> <p>I have a <code>ServiceConfig</code> element defined like so: </p> <pre><code>public class ServiceConfig : ConfigurationElement { public ServiceConfig() {} public ServiceConfig(int port, string reportType) { Port = port; ReportType = reportType; } [ConfigurationProperty("Port", DefaultValue = 0, IsRequired = true, IsKey = true)] public int Port { get { return (int) this["Port"]; } set { this["Port"] = value; } } [ConfigurationProperty("ReportType", DefaultValue = "File", IsRequired = true, IsKey = false)] public string ReportType { get { return (string) this["ReportType"]; } set { this["ReportType"] = value; } } } </code></pre> <p>And I have a <code>ServiceCollection</code> defined like so: </p> <pre><code>public class ServiceCollection : ConfigurationElementCollection { public ServiceCollection() { Console.WriteLine("ServiceCollection Constructor"); } public ServiceConfig this[int index] { get { return (ServiceConfig)BaseGet(index); } set { if (BaseGet(index) != null) { BaseRemoveAt(index); } BaseAdd(index, value); } } public void Add(ServiceConfig serviceConfig) { BaseAdd(serviceConfig); } public void Clear() { BaseClear(); } protected override ConfigurationElement CreateNewElement() { return new ServiceConfig(); } protected override object GetElementKey(ConfigurationElement element) { return ((ServiceConfig) element).Port; } public void Remove(ServiceConfig serviceConfig) { BaseRemove(serviceConfig.Port); } public void RemoveAt(int index) { BaseRemoveAt(index); } public void Remove(string name) { BaseRemove(name); } } </code></pre> <p>The part I am missing is what to do for the handler. Originally, I tried to implement an <code>IConfigurationSectionHandler</code> but found two things:</p> <ol> <li>it didn't work</li> <li>it's deprecated. </li> </ol> <p>I'm completely lost now on what to do so I can read my data from config. Any help please!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload