27

My app.config file is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="ProcessConfiguration" type="Configuration.ProcessConfigurationSection, Configuration" />
    </configSections>
    <ProcessConfiguration>
        <processes>
            <process name="Process1" />
        </processes>
    </ProcessConfiguration>
</configuration>

I have the following (separate) classes to get the configuration:

namespace Configuration
{
    using System.Configuration;

    public class ProcessesConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("processes", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(ProcessCollection))]
        public ProcessCollection Processes
        {
            get
            {
                return (ProcessCollection)base["processes"];
            }
        }
    }
}

namespace Configuration
{
    using System.Configuration;

    public class ProcessCollection : ConfigurationElementCollection
    {
        public ProcessConfig this[int index]
        {
            get
            {
                return (ProcessConfig)BaseGet(index);
            }

            set
            {
                BaseAdd(index, value);
            }
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ProcessConfig)element).Name;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ProcessConfig();
        }
    }
}

namespace Configuration
{
    using System.Configuration;

    public class ProcessConfig : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name 
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                this["name"] = value;
            }
        }
    }
}

However when I hit this line of code:

var processConfigurationSection = ConfigurationManager.GetSection("ProcessConfiguration") as ProcessesConfigurationSection;

I get the error which states:

An error occurred creating the configuration section handler for ProcessConfiguration: Could not load type 'Configuration.ProcessConfigurationSection' from assembly 'Configuration'.

I'm completely stumped, if anyone can help me out I'd really appreciate it.

Andrii Kalytiiuk
  • 1,501
  • 14
  • 26
RichardB
  • 606
  • 2
  • 12
  • 22
  • You'll want to debug this using the [Fusion log viewer.](http://msdn.microsoft.com/en-us/library/e74a18c4.aspx) Just make sure to run it as admin, turn on the log, and reboot before attempting to debug. You'll see where the CLR is looking for the assembly, and what version, and from there determine why it isn't being found, if that's the issue. –  Sep 30 '13 at 13:17
  • what is the name of the DLL you are building? – Justin Harvey Sep 30 '13 at 13:18
  • Is your type `ProcessesConfigurationSection` defined in the entry assembly? – Andrii Kalytiiuk Sep 30 '13 at 13:19
  • I just have it in a console application just now, named 'Configuration'. Andrii, where would I look to see if the type is defined in the entry assembly? Thanks. – RichardB Sep 30 '13 at 13:22

2 Answers2

52

In the line:

<section name="ProcessConfiguration" type="Configuration.ProcessConfigurationSection, Configuration" />

The name 'Configuration' should refer to the DLL that you re building, please try checking this and correct if needed.

Also I think you may have a typo, in your code the type name is:

ProcessesConfigurationSection

(Note the Processes vs Process)

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
  • Thanks, the name of the compiled binary is Configuration.exe so I think that means I have that line correct? – RichardB Sep 30 '13 at 13:24
  • 1
    See addition, I think this may be a typo – Justin Harvey Sep 30 '13 at 13:30
  • In my case 'Confirguration' is the namespace that I put the configuration handler in , which is not in a separate DLL/Assembly . – LostNomad311 Oct 09 '14 at 15:36
  • Oddly enough, it was the "you may have a typo" that solved my problem. I got this error because I had the capitalization wrong. – Bobson Feb 17 '15 at 22:39
  • I didn't know you needed the ", Configuration" at the end. I was following this MSDN tutorial: https://learn.microsoft.com/en-us/previous-versions/aspnet/2tw134k3(v=vs.100) and it doesn't mention that at all. – David Apr 13 '22 at 17:34
3

This error has been raised because the invoking assembly couldn't load the class type in the specified assembly. This error can be caused by a space after the type name (which has happened to me), for example the following config section

type="Configuration.ProcessConfigurationSection , Configuration"

will generate this error too.

Ali Fattahian
  • 495
  • 1
  • 6
  • 24