简体   繁体   中英

Unable to create a custom section for web.config

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.

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM