无法创建web.config文件自定义栏目(Unable to create a custom se

2019-07-05 04:14发布

我创建的web.config文件自定义栏目,但它无法加载是要管理一节我的自定义类型。

下面是定义:

<configSections>
<section
        name="MembershipProviders"
        type="MyApp.BusinessObjects.MembershipProviderFactory.MembershipProvidersSection"
        allowLocation="true"
        allowDefinition="Everywhere"
      />
</configSections>


namespace MyApp.BusinessObjects
{
    public class MembershipProviderFactory
    {
        internal virtual IMembershipProvider Create()
        {
        }

        public class MembershipProvidersSection : ConfigurationSection
        {
            public class AddElement: ConfigurationElement
            {
                [ConfigurationProperty("name", IsKey  = true, IsRequired = true)]
                public string Name
                {
                    get
                    {
                        return this["name"].ToString();
                    }
                    set
                    {
                        this["name"] = value;
                    }
                }

                [ConfigurationProperty("type", IsRequired = true)]
                public string FullyQualifiedTypeName
                {
                    get
                    {
                        return this["type"].ToString();
                    }
                    set
                    {
                        this["type"] = value;
                    }
                }
            }

            public class AddElementCollection : ConfigurationElementCollection
            {
                protected override ConfigurationElement CreateNewElement()
                {
                    return new AddElement();
                }

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


            [ConfigurationProperty("currentProvider", IsRequired = false)]
            public string CurrentProvider
            {
                get
                {
                    return this["currentProvider"].ToString();
                }
                set
                {
                    this["currentProvider"] = value;
                }
            }

            [ConfigurationProperty("add", IsRequired = true, IsDefaultCollection = true)]
            public AddElementCollection Instances
            {
                get { return (AddElementCollection)this["add"]; }
                set { this["add"] = value; }
            }
        }
    }
}

我得到一个运行时异常,上面写着:

发生错误创建MembershipProviders配置节处理程序:无法加载类型“MyApp.BusinessObjects.MembershipProviderFactory.MembershipProvidersSection”。

更新

我也包含在配置文件中的实际部分,如下所示:

<MembershipProviders currentProvider ="DefaultMembershipProvider" />

我仍然得到同样的异常。

Answer 1:

您需要指定集名称为部分type的属性:

<section
    name="MembershipProviders"
    type="Namespace.TheCustomSection, TheAssemblyNameGoesHere"
    allowLocation="true"
    allowDefinition="Everywhere"
/>

编辑
我没有注意到MembershipProvidersSection类是嵌套类。
该类型的名称应该是:

MyApp.BusinessObjects.MembershipProviderFactory+MembershipProvidersSection


Answer 2:

这是因为丢失你在哪里声明式组装的名字:

MyApp.BusinessObjects.MembershipProviderFactory.MembershipProvidersSection,?

看看我的关于自定义配置的职位之一: C#WCF System.Configuration.ConfigurationErrorsException:无法识别的元素“ManagedService”



文章来源: Unable to create a custom section for web.config