Search code examples
asp.netconfigurationcustom-tag

Custom tags in App.config - type of my ConfigurationSection class is not recognised


I have followed (almost) to the letter the example from MSDN to create custom tags in my App.config (find documentation here: https://msdn.microsoft.com/en-us/library/2tw134k3.aspx) but I am getting this error:

An unhandled exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.configuration.dll

Additional information: An error occurred creating the configuration section handler for MyServiceGroup/ServiceUpdater: Could not load type 'MyNamespace.ServiceUpdaterSection' from assembly 'System.configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f33d50a4a'.

and the error is triggered on this line (inside Main from my Console App when I try to make use of the custom information from App.config):

MyNamespace.ServiceUpdaterSection serviceUpdaterSection =
(ServiceUpdaterSection)ConfigurationManager.GetSection("MyServiceGroup/ServiceUpdater");

and from the error message I can already see this is because it's trying to locate MyNamespace.ServiceUpdaterSection inside System.Configuration, on the contrary, it should find this class (ServiceUpdaterSection) inside MyNamespace as I have given it the fully qualified name.

Here is how my App.config looks:

<configSections>
    <sectionGroup name="MyServiceGroup">
      <section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection"/>
    </sectionGroup>
    </configSections>

and further below inside App.config I have:

<MyServiceGroup>
    <ServiceUpdater>
      <licenseKey id="blablabla"/>
    </ServiceUpdater>     

As for the ServiceUpdaterSection class, it looks as follows:

namespace MyNamespace
{
    public class LicenseKeyElement : ConfigurationElement
    {
        [ConfigurationProperty("id")]
        public string Id
        {
            get
            {
                return (string)this["id"];
            }
            set
            {
                this["id"] = value;
            }
        }
    }
    public class ServiceUpdaterSection : ConfigurationSection
    {
        [ConfigurationProperty("licenseKey")]
        public LicenseKeyElement LicenseKey
        {
            get
            {
                return (LicenseKeyElement)this["licenseKey"];
            }
            set
            {
                this["licenseKey"] = value;
            }
        }
    }

}

What are your thoughts on this please?

Thank you.


Solution

  • The error was here:

     <section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection"/>
    

    which should have been:

     <section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection, MyNamespace"/>
    

    Will leave it in case someone else encounters the same issue.