尝试在 Web.config 中创建自定义部分

Try to create a custom section in Web.config

我正在尝试在 web.config 中创建一个自定义部分。我的 web.config 中有自定义 class 和声明部分。

当我 运行 我的代码时,我收到此错误“...不继承自 System.Configuration.IConfigurationSectionHandler”。

MSDN 文档指出此接口现已弃用。
我不明白这是怎么回事。
仅供参考,我用这个(http://blogs.perficient.com/microsoft/2017/01/4-easy-steps-to-custom-sections-in-web-config/ 作为创建自定义部分的指南。

Here is my web.config    



<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name ="BDKConfigs" type="SPS.CardDecryption.Services.BDKConfigs" allowLocation="true" allowDefinition="Everywhere" />
  </configSections>

Here is my custom section in the web.config      



 <BDKConfigs>
        <!--collection-->
        <BDK>
          <!--elements-->
          <add name="IDTechPublicBDK" value="0123456789ABCDEFFEDCBA9876543210"/>
          <add name="IDTechProdBDK" value=""/>
         </BDK>
    </BDKConfigs>

this is my custom class      


using System.Configuration;

namespace myNameSpace
{
    //This class reads the defined config section (if available) and stores it locally in the static _Config variable.  

    public class BDKConfigs 
    {
        public static BDKConfigsSection _Config = ConfigurationManager.GetSection("BDKConfigs") as BDKConfigsSection;

        public static BDKElementCollection GetBDKGroups()
        {
            return _Config.BDKConfigs;
        }


    }

     public class BDKConfigsSection : ConfigurationSection
    {
        //Decorate the property with the tag for your collection.
        [ConfigurationProperty("BDK")]
        public BDKElementCollection BDKConfigs
        {
            get { return (BDKElementCollection)this["BDK"]; }
        }
    }

    //Extend the ConfigurationElementCollection class.
    //Decorate the class with the class that represents a single element in the collection. 
    [ConfigurationCollection(typeof(BDKElement))]
    public class BDKElementCollection : ConfigurationElementCollection
    {
        public BDKElement this[int index]
        {
            get { return (BDKElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);

                BaseAdd(index, value);
            }
        }

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

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

    //Extend the ConfigurationElement class.  This class represents a single element in the collection. 
    //Create a property for each xml attribute in your element.  
    //Decorate each property with the ConfigurationProperty decorator.  See MSDN for all available options.
    public class BDKElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("queryString", IsRequired = false)]
        public string Value
        {
            get { return (string)this["Value"]; }
            set { this["Value"] = value; }
        }

    }
}

我更新了答案:

 public class BDKConfigsSection : ConfigurationSection
{
    [ConfigurationProperty("BDK")]
    public BDKElementCollection BDKConfigs
    {
        get { return (BDKElementCollection) base["BDK"]; }
    }
}

[ConfigurationCollection(typeof(BDKElement))]
public class BDKElementCollection : ConfigurationElementCollection
{
    public BDKElement this[int index]
    {
        get { return (BDKElement)BaseGet(index); }
        set
        {
            if (BaseGet(index) != null)
                BaseRemoveAt(index);

            BaseAdd(index, value);
        }
    }

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

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((BDKElement)element).Name;
    }
    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.BasicMapAlternate; }
    }
    protected override string ElementName => "BDKItem";
}

public class BDKElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get { return (string)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("queryString", IsRequired = false)]
    public string Value
    {
        get { return (string)this["queryString"]; }
        set { this["queryString"] = value; }
    }

}

那个工作正常:

  <BDKConfigsSection>
<BDK>
  <BDKItem name="IDTechPublicBDK" queryString="0123456789ABCDEFFEDCBA9876543210"/>
  <BDKItem name="IDTechProdBDK" queryString=""/>
</BDK> </BDKConfigsSection>

问题是我在 web.config 中指定了错误的类型。当我将 web.config 更改为

type="SPS.CardDecryption.Services.BDKConfigsSection ,工作起来很有魅力。