Search code examples
c#custom-configuration

What is the correct Custom Configuration code for my XML?


Assuming the following XML, what is the correct Custom Configuration code?:

<ServicesMonitor>
  <serviceTestGroups>
    <serviceTestGroup name="foo">
      <serviceTest uri="http://server1/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
      <serviceTest uri="http://server2/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
      <serviceTest uri="http://server3/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
    </serviceTestGroup>
  </serviceTestGroups>
</ServicesMonitor>

I know I need:

  • ServiceTestElement : ConfigurationElement

And I think I need:

  • ServiceTestElementCollection : ConfigurationElementCollection
  • ServiceTestGroupsElement : ConfigurationSection

Solution

  • This isn't the exact XML you have above but it is very close:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="servicesMonitor" type="TestConfigurationElement.ServicesMonitorSection, TestConfigurationElement"/>
      </configSections>
      <servicesMonitor>
        <serviceTestGroups>
          <add name="foo">
            <serviceTests>
              <add uri="http://server1/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
              <add uri="http://server2/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
              <add uri="http://server3/GIS/rest/services/geocode/FindAddress/GeocodeServer?f=json" expectedResponseTime="1000" />
            </serviceTests>
          </add>
        </serviceTestGroups>
      </servicesMonitor>
    </configuration>
    

    Here is the code:

    using System;
    using System.Configuration;
    
    namespace TestConfigurationElement
    {
        public class ServicesMonitorSection : ConfigurationSection
        {
            [ConfigurationProperty("serviceTestGroups", IsRequired = true)]
            public ServiceTestGroupElementCollection ServiceTestGroups
            {
                get { return (ServiceTestGroupElementCollection)this["serviceTestGroups"]; }
                set { this["serviceTestGroups"] = value; }
            }
        }
    
        public class ServiceTestGroupElement : ConfigurationElement
        {
            [ConfigurationProperty("name", IsRequired = true)]
            public string Name
            {
                get { return (string)this["name"]; }
                set { this["name"] = value; }
            }
    
            [ConfigurationProperty("serviceTests", IsRequired = true)]
            public ServiceTestElementCollection ServiceTests
            {
                get { return (ServiceTestElementCollection)this["serviceTests"]; }
                set { this["serviceTests"] = value; }
            }
        }
    
        public class ServiceTestGroupElementCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new ServiceTestGroupElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((ServiceTestGroupElement)element).Name;
            }
        }
    
        public class ServiceTestElement : ConfigurationElement
        {
            [ConfigurationProperty("uri", IsRequired = true)]
            public Uri Uri
            {
                get { return (Uri)this["uri"]; }
                set { this["uri"] = value; }
            }
    
            [ConfigurationProperty("expectedResponseTime", IsRequired = true)]
            public int ExpectedResponseTime
            {
                get { return (int)this["expectedResponseTime"]; }
                set { this["expectedResponseTime"] = value; }
            }
        }
    
        public class ServiceTestElementCollection : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new ServiceTestElement();
            }
    
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((ServiceTestElement)element).Uri;
            }
        }
    
        public static class Program
        {
            public static void Main(string[] args)
            {
                ServicesMonitorSection section = (ServicesMonitorSection)ConfigurationManager.GetSection("servicesMonitor");
            }
        }
    }