Unable to create a custom section for web.config

我与影子孤独终老i 提交于 2019-12-01 13:32:22

问题


I created a custom section in the web.config file but it isn't able to load my custom type that is going to manage the section.

Here are the definitions:

<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; }
            }
        }
    }
}

I get a run-time exception that says:

An error occurred creating the configuration section handler for MembershipProviders: Could not load type 'MyApp.BusinessObjects.MembershipProviderFactory.MembershipProvidersSection'.

Update

I also included the actual section in the config file as follows:

<MembershipProviders currentProvider ="DefaultMembershipProvider" />

I still get the same exception.


回答1:


You need to specify the assembly name as part of the type attribute:

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

EDIT
I didn't notice that the MembershipProvidersSection class is a nested class.
The type name should be:

MyApp.BusinessObjects.MembershipProviderFactory+MembershipProvidersSection



回答2:


You are missing assembly name where you are declaring type:

MyApp.BusinessObjects.MembershipProviderFactory.MembershipProvidersSection,?

Take a look at one of my posts about custom configuration: C# WCF System.Configuration.ConfigurationErrorsException: Unrecognized element 'ManagedService'



来源:https://stackoverflow.com/questions/13862125/unable-to-create-a-custom-section-for-web-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!