Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An error occurred creating the configuration section handler

I have a dot.NET 4.0 web application with a custom section defined:

<configuration>
    <configSections>
    <section name="registrations" type="System.Configuration.IgnoreSectionHandler, System.Configuration, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="true" restartOnExternalChanges="true" allowLocation="true"/>
    ....

at the end of the web.config file I have the respective section:

  ....
  <registrations>
    .....
  </registrations>
</configuration>

Every time I call System.Configuration.ConfigurationManager.GetSection("registrations"); I get the following exception:

An error occurred creating the configuration section handler for registrations: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047) (C:\...\web.config line 13)

I'm also using Unity but don't know if that's in any way related to the error.

Have you faced this error before? How can I fix it? Do I need to replace the IgnoreSectionHandler with something else?

like image 216
JohnDoDo Avatar asked Apr 29 '13 17:04

JohnDoDo


2 Answers

Given this app.config:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <section name="registrations" type="MyApp.MyConfigurationSection, MyApp" />
    </configSections>
    <registrations myValue="Hello World" />
</configuration>

Then try using this:

namespace MyApp
{
    class Program
    {
        static void Main(string[] args) {
            var config = ConfigurationManager.GetSection(MyConfigurationSection.SectionName) as MyConfigurationSection ?? new MyConfigurationSection();

            Console.WriteLine(config.MyValue);

            Console.ReadLine();
        }
    }

    public class MyConfigurationSection : ConfigurationSection
    {
        public const String SectionName = "registrations";

        [ConfigurationProperty("myValue")]
        public String MyValue {
            get { return (String)this["myValue"]; }
            set { this["myValue"] = value; }
        }

    }
}
like image 85
Doug Wilson Avatar answered Oct 20 '22 18:10

Doug Wilson


You are missing the Namespace in the type attribute of your section in App.Config. Infact you dont need the full assembly info in there either. only the namespace is enough

Updated 1

  yourcustomconfigclass config =(yourcustomconfigclass)System.Configuration.ConfigurationManager.GetSection(
    "registrations");

and in config file only write

   <section name="registrations" type="System.Configuration.IgnoreSectionHandler" requirePermission="true" restartOnExternalChanges="true" allowLocation="true"/>
like image 23
Shafqat Masood Avatar answered Oct 20 '22 16:10

Shafqat Masood