自定义web.config部分处理程序 [英] Custom web.config Section Handler

查看:176
本文介绍了自定义web.config部分处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个自定义节处理程序之前,但我面临着一个问题,我似乎不能想起来。我有这样的配置部分:

I have designed a custom section handler before but I'm faced with a problem that I can not seem to think up. I have a configuration section like this:

<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 in order to successfully execute the following code?

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


推荐答案

您应该使用 ConfigurationElementCollection and KeyValueConfigurationCollection 用于要用作集合的元素。在这种情况下,您必须创建一个元素的集合,每个元素都有一个KeyValueConfigurationCollection。所以,不是你以上的XML配置,你会有这样的更多:

You should look into using ConfigurationElementCollection and KeyValueConfigurationCollection for the Elements you want to use as a collection. In this case, you will have to make a collection of elements, each element having a KeyValueConfigurationCollection. So, instead of the XML config that you have above, you will have something more like this:

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

您可以重复使用queries元素,这将是您的KeyValueConfigurationCollection,

You can re-use the "queries" element, which will be your KeyValueConfigurationCollection, for each "provider."

Google快速搜索取得了

A quick Google search yielded this article on MSDN, which also may be of help.

编辑 - 代码示例

您的根段定义将如下所示:

Your root section definition will be like this:

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

然后,您的提供者ElementCollection:

Then, your 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元素:

Then, your Provider element:

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有点使它的工作正确,但我认为这将是一般的想法。然后,当你在代码中访问这个东西时,你会做这样的事情:

You're probably going to have to mess around with the KeyValueConfigurationCollection a bit to make it work right, but I think this would be the general idea. Then, when accessing this stuff in your code, you would do something like this:

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

Hope that helps. Now just don't ask me to translate it to VB :-D

这篇关于自定义web.config部分处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆