自定义的web.config节处理(Custom web.config Section Handle

2019-09-02 19:07发布

我之前已经设计了一个自定义节处理程序,但我面对的是我似乎无法想出一个问题。 我有这样的配置部分:

<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>

我创建了以下处理类:

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;
            }
        }
    }
}

我需要什么样的代码在这些类,才能成功执行下面的代码?

string query = ProvidersSection["tasks"].Queries["Insert"];

Answer 1:

你应该考虑使用ConfigurationElementCollection中和KeyValueConfigurationCollection你想作为一个集合使用的元素。 在这种情况下,你将不得不作出元素的集合,具有KeyValueConfigurationCollection每个元素。 因此,而不是你有上面的XML配置,你将有更多的东西是这样的:

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

您可以重新使用“查询”元素,这将是你的KeyValueConfigurationCollection,每个“供应商”。

快速谷歌搜索产生了这篇文章在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"; }
    }
}

然后,你的供应商元素:

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



文章来源: Custom web.config Section Handler