Web.config custom section handler

I developed a custom section handler before, but I ran into a problem that I can't think of. I have a configuration section as follows:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    <configSections>
        <section name="providers" requirePermission="false" type="MyProject.Configuration.ProvidersSection"/>
    </configSections>

    <providers>
        <provider name="products">
            <queries>
                <add name="insert" value="InsertProduct" />
                <add name="retrieve" value="GetProduct" />
                <add name="collect" value="GetProducts" />
                <add name="update" value="UpdateProduct" />
                <add name="delete" value="DeleteProduct" />
            <queries>
        </provider>
        <provider name="tasks">
            <queries>
                <add name="insert" value="InsertTask" />
                <add name="retrieve" value="GetTaskById" />
                <add name="collect" value="GetMultipleTasks" />
                <add name="update" value="UpdateTask" />
                <add name="delete" value="DeleteTask" />
            <queries>
        </provider>
        <provider name="events">
            <queries>
                <add name="insert" value="AddEvent" />
                <add name="retrieve" value="GetEvent" />
                <add name="collect" value="GetEvents" />
                <add name="update" value="UpdateEvent" />
                <add name="delete" value="DeleteEvent" />
            <queries>
        </provider>
    </providers>
</configuration>

I created the following handler classes:

using System.Configuration;

namespace MyProject.Configuration
{
    public class ProvidersSection : ConfigurationSection
    {
        public new Element this[string key]
        {
            get
            {

            }
        }
    }

    [ConfigurationCollection(typeof(ProviderElement))]
    public class ProvidersCollection : ConfigurationElementCollection
    {

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

        protected override object GetElementKey(ConfigurationElement element)
        {
            return element.ElementInformation.Properties["name"].Value;
        }

        public ProviderElement this[string key]
        {
            get
            {
                return (ProviderElement)base.BaseGet(key);
            }
        }
    }

    public class ProviderElement : ConfigurationElement
    {
        public string this[string name]
        {
            get
            {
                return string.Empty;
            }
        }
    }
}

What code do I need in these classes to successfully execute the following code?

string query = ProvidersSection["tasks"].Queries["Insert"];
+3
source share
1 answer

ConfigurationElementCollection KeyValueConfigurationCollection , . , KeyValueConfigurationCollection. , XML :

<providers>
    <provider key="products">
        <queries>
             <add key="whatever" value="stuff" />
             ...etc...
        <queries>
    <provider>
</providers>

"", KeyValueConfigurationCollection, "".

Google MSDN, .

-

:

public class ProviderConfiguration : ConfigurationSection
{
    [ConfigurationProperty("Providers",IsRequired = true)]
    public ProviderElementCollection Providers
    {
        get{ return (ProviderElementCollection)this["Providers"]; }
        set{ this["Providers"] = value; }
    }
}

ElementCollection:

public class ProviderCollection : ConfigurationElementCollection
{
    public ProviderElement this[object elementKey]
    {
        get { return BaseGet(elementKey); }
    }

    public void Add(ProviderElement provider)
    {
        base.BaseAdd(provider);
    }

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

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ProviderElement)element).Key;
    }

    protected override string ElementName
    {
        get { return "Provider"; }
    }
}

Provider:

public class Provider : ConfigurationElement
{
    [ConfigurationProperty("Key",IsRequired = true, IsKey = true)]
    public string Key
    {
        get { return (string) this["Key"]; }
        set { this["Key"] = value; }
    }

    [ConfigurationProperty("Queries", IsRequired = true)]
    public KeyValueConfigurationCollection Queries
    {
        get { return (KeyValueConfigurationCollection)this["Queries"]; }
        set { this["Queries"] = value; }
    }
}

, , KeyValueConfigurationCollection, , , . , , - :

var myConfig = (ProviderConfiguration)ConfigurationManager.GetSection("Providers");
//and to access a single value from, say, your products collection...
var myValue = myConfig.Providers["Products"].Queries["KeyForKeyValuePairing"].Value;

, . VB:-D

+4

Source: https://habr.com/ru/post/1721522/


All Articles