无法为web.config创建自定义部分

时间:2012-12-13 14:38:54

标签: c# configuration web-config app-config configuration-files

我在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" />

我仍然得到同样的例外。

2 个答案:

答案 0 :(得分:1)

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

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

修改
我没注意到MembershipProvidersSection类是嵌套类 类型名称应为:

MyApp.BusinessObjects.MembershipProviderFactory+MembershipProvidersSection

答案 1 :(得分:1)

您缺少声明类型的程序集名称:

MyApp.BusinessObjects.MembershipProviderFactory.MembershipProvidersSection,?

看一下我关于自定义配置的帖子: C# WCF System.Configuration.ConfigurationErrorsException: Unrecognized element 'ManagedService'