0

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.

Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
  • What does your membership providers section actually look like in Web.config? – levelnis Dec 13 '12 at 15:01
  • I haven't included one yet. Can a section be optional? That is, can I have the declaration of the section but not the actual section itself present in the web.config? – Water Cooler v2 Dec 13 '12 at 15:04
  • The section can be optional. You can add a ConfigurationProperty attribute to a configuration section or element with IsRequired set to false: `[ConfigurationProperty("elementName", IsRequired = false)]` – levelnis Dec 13 '12 at 15:16
  • Thank you. As per that, then, I didn't do anything wrong. However, I have still included the section now. I have updated the original question. – Water Cooler v2 Dec 13 '12 at 15:22

2 Answers2

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
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
  • Not if it is the current assembly? – Water Cooler v2 Dec 13 '12 at 15:03
  • @WaterCoolerv2: There is no "current assembly" when the config file is parsed. – Richard Deeming Dec 13 '12 at 15:05
  • Exactly. So, it is not a requirement to pass the assembly name in the config when the type you want to load is inside the same assembly. – Water Cooler v2 Dec 13 '12 at 15:07
  • 1
    @WaterCoolerv2: No, the assembly name **is** required. There is no concept of a "current assembly". You must **always** specify the assembly name. – Richard Deeming Dec 13 '12 at 15:08
  • @WaterCoolerv2, Richard is right. It will not work without assembly name. – Gregor Primar Dec 13 '12 at 15:09
  • This article implies that the assembly name is required only if the config section handler code is outside the currently executing assembly. http://msdn.microsoft.com/en-us/library/2tw134k3%28v=vs.100%29.aspx – Water Cooler v2 Dec 13 '12 at 15:11
  • Underneath the section entitled 'To add a custom section handler to an ASP.NET configuration file', point 2: Ensure that the type attribute of the section element matches the manifest of the assembly (ensure that you specify both the correct namespace and type name). – levelnis Dec 13 '12 at 15:20
  • @WaterCoolerv2: See my edit; the type name for a nested class uses a `+` instead of a `.` between the parent class name and the nested class name. – Richard Deeming Dec 13 '12 at 15:22
  • Thank you, Richard. With the + it worked now. That was the problem. – Water Cooler v2 Dec 13 '12 at 15:25
1

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'

Community
  • 1
  • 1
Gregor Primar
  • 6,759
  • 2
  • 33
  • 46