Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigurationManager.GetSection Gives Error "Could not load type....from assembly..."

Tags:

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.

like image 410
RichardB Avatar asked Sep 30 '13 13:09

RichardB


People also ask

Does ConfigurationManager work in .NET core?

ConfigurationManager was added to support ASP.NET Core's new WebApplication model, used for simplifying the ASP.NET Core startup code.

What is the use of ConfigurationManager in C#?

The ConfigurationManager class includes members that enable you to perform the following tasks: Read a section from a configuration file. To access configuration information, call the GetSection method. For some sections such as appSettings and connectionStrings , use the AppSettings and ConnectionStrings classes.

What is the use of ConfigurationManager AppSettings?

ConfigurationManager Class (System. Configuration) Provides access to configuration files for client applications.


2 Answers

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)

like image 78
Justin Harvey Avatar answered Oct 05 '22 11:10

Justin Harvey


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.

like image 42
Ali Fattahian Avatar answered Oct 05 '22 09:10

Ali Fattahian