6

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?

JohnDoDo
  • 4,720
  • 8
  • 31
  • 47

2 Answers2

13

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

    }
}
Doug Wilson
  • 4,185
  • 3
  • 30
  • 35
  • 4
    I was missing the namespace ",MyApp" in the section name (
    ) declaration. Cheers!
    – bizl Feb 27 '15 at 01:17
  • I'd changed the case of my name spaces e.g. MyApp.Configuration to Myapp.Configuration - remember that assembly names are case sensitive. (Thanks @bizl your comment gave me the idea to check it) – Aligma Oct 18 '15 at 03:18
  • 1
    I needed this super simple sample to get past a brain lockup. Adding namespace did it, plus this example gave insight enough to see that the rest of my code was ok. Thanks! – Culme Sep 12 '18 at 09:20
  • I'm working on reimplementing a service from on-prem to cloud and wanted to hook into one of the methods inside the on-prem solution in a console app for testing. I was getting nowhere until I added the namespace where the configuration code was located. – Wildcat Matt Jun 19 '23 at 21:52
9

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"/>
Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23
  • I just noticed that! Crap... fixed it and now I have `System.Configuration.IgnoreSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089`. I'm getting no error but the section comes back as `null`. Any idea how to get the section content? – JohnDoDo Apr 29 '13 at 18:19
  • Finally figured out where the null was coming from. I've been dumb once by messing the type value and them being dumb a second time by not understanding why I get a null. Well, it's an IgnoreSectionHandler who's Create method returns the null. Doh! – JohnDoDo Apr 30 '13 at 07:49