簡體   English   中英

自定義web.config節處理程序

[英]Custom web.config Section Handler

我之前設計了一個自定義部分處理程序,但我遇到了一個我似乎無法想到的問題。 我有一個像這樣的配置部分:

<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"];

您應該考慮對要用作集合的Elements使用ConfigurationElementCollectionKeyValueConfigurationCollection 在這種情況下,您將必須創建元素集合,每個元素都具有KeyValueConfigurationCollection。 因此,您將擁有更多類似的內容,而不是您上面的XML配置:

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

您可以為每個“提供者”重復使用“queries”元素,它將是您的KeyValueConfigurationCollection。

谷歌快速搜索在MSDN上發表這篇文章 ,這也可能有所幫助。

編輯 - 代碼示例

您的根節定義將如下所示:

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

然后,您的Providers 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM