Share via


complex custom configuration section in Web.Config in asp.net sample

lets say i  have a custom configuration section like this

 

 <configSections> 
        <section name="ProductsEndpoints" type ="abcd.ProductsEndpointsConfiguration" /> 
 </configSections> 
 
 <ProductsEndpoints configSource="ProductsEndpoints.config" />

 

and My configuration is a complex xml like

 

// ProductsEndpoints.config
<?xml version="1.0"?>
<ProductsEndpoints>
 <Products>
  <Product Name="abcd" MemberKey="*">
   <Endpoints>
    <Endpoint ServiceName="dtd" Uri="https://xxx-dev-app03.devid.local/dfff/v1/mbemmMrs" />
    <Endpoint ServiceName="hhh" Uri="https://ppo-dev-app03.devid.local/gss/v1/Measures" />
    <Endpoint ServiceName="jjj" Uri="https://fff.hh.com/uu/v1" />
   </Endpoints>
  </Product>
  <Product Name="jyy" MemberKey="de">
   <Endpoints>
    <Endpoint ServiceName="ggg" Uri="https://mht-dev-db02/acce/v1/wwqbcdValue" />
   </Endpoints>
  </Product>
  <Product Name="test" MemberKey="test">
   <Endpoints>
    <Endpoint ServiceName="test" Uri="https://www.blahblah.com" />
   </Endpoints>
  </Product>
 </Products>
</ProductsEndpoints>

 

 

so here are the classes needed to read/write into this configuration

 

    public class ProductsEndpointsConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("Products", IsRequired = true, IsKey = false)]
        public ProductCollection Products
        {
            get
            {
                return (ProductCollection)base["Products"];
            }
            set
            {
                base["Products"] = value;
            }
        }

        public override bool IsReadOnly()
        {
            return false;
        }
    }

    [ConfigurationCollection(typeof(Product), AddItemName = "Product", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class ProductCollection : ConfigurationElementCollection
    {
        private const string ItemName = "Product";

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMapAlternate; }
        }

        protected override string ElementName
        {
            get { return ItemName; }
        }

        protected override bool IsElementName(string elementName)
        {
            return (elementName == ItemName);
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((Product)element).Name;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new Product();
        }

        new public Product this[string productName]
        {
            get { return (Product)base.BaseGet(productName); }
        }

        public override bool IsReadOnly()
        {
            return false;
        }

    }

 

    public class EndPointsConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("Endpoints", IsRequired = true, IsKey = false)]
        public EndpointCollection Endpoints
        {
            get
            {
                return (EndpointCollection)base["Endpoints"];
            }
            set
            {
                base["Endpoints"] = value;
            }
        }

        public override bool IsReadOnly()
        {
            return false;
        }
    }

    [ConfigurationCollection(typeof(Endpoint), AddItemName = "Endpoint", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class EndpointCollection : ConfigurationElementCollection
    {
        private const string ItemName = "Endpoint";

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMapAlternate; }
        }

        protected override string ElementName
        {
            get { return ItemName; }
        }

        protected override bool IsElementName(string elementName)
        {
            return (elementName == ItemName);
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((Endpoint)element).ServiceName;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new Endpoint();
        }

        new public Endpoint this[string serviceName]
        {
            get { return (Endpoint)base.BaseGet(serviceName); }
        }

        public override bool IsReadOnly()
        {
            return false;
        }

    }

 

    public class Product : ConfigurationElement
    {
        [ConfigurationProperty("Name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return (string)base["Name"]; }
            set { base["Name"] = value; }
        }

        [ConfigurationProperty("MemberKey", IsRequired = true)]
        public string MemberKey
        {
            get { return (string)base["MemberKey"]; }
            set { base["MemberKey"] = value; }
        }

        [ConfigurationProperty("Endpoints", IsRequired = false, IsKey = false)]
        public EndpointCollection EndPoints
        {
            get
            {
                return ((EndpointCollection)(base["Endpoints"]));
            }
            set
            {
                base["Endpoints"] = value;
            }
        }

        public override bool IsReadOnly()
        {
            return false;
        }
    }

    public class Endpoint : ConfigurationElement
    {
        [ConfigurationProperty("ServiceName", IsRequired = true, IsKey = true)]
        public string ServiceName
        {
            get { return (string)base["ServiceName"]; }
            set { base["ServiceName"] = value; }
        }

        [ConfigurationProperty("Uri", IsRequired = true)]
        public string Uri
        {
            get { return (string)base["Uri"]; }
            set { base["Uri"] = value; }
        }

        public override bool IsReadOnly()
        {
            return false;
        }
    }