Custom ConfigurationSection Error: Unable to load type


kick it on DotNetKicks.com

I was creating a custom ConfigurationSection for an App.Config file.  Thinking I had done everything correctly (this really isn’t that hard) I kept getting this error.

"An error occurred creating the configuration section handler for LogManagerGroup/LogManager: Unable to load type 'Core.Logging.Configuration.LogManagerSection, Core.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=br549br549br549' because it is not public.”

I tried everything I knew to debug this thinking the problem was with the config file but it turned out to be code related.  Here’s the bad example:

   1: public class MyCustomSection : ConfigurationSection
   2: {
   3:     [ConfigurationProperty("IsDebugEnabled", DefaultValue = "true", IsRequired = true)]
   4:     public Boolean IsDebugEnabled
   5:     {
   6:         get
   7:         {
   8:             return (Boolean)this["IsDebugEnabled"];
   9:         }
  10:         set
  11:         {
  12:             this["IsDebugEnabled"] = value;
  13:         }
  14:     }
  15: }

Here’s the fix (just add a constructor):

   1: public class MyCustomSection : ConfigurationSection
   2: {
   3:     // Adding Constructor fixes this error
   4:     public MyCustomSection()
   5:     {
   6:  
   7:     }
   8:  
   9:     [ConfigurationProperty("IsDebugEnabled", DefaultValue = "true", IsRequired = true)]
  10:     public Boolean IsDebugEnabled
  11:     {
  12:         get
  13:         {
  14:             return (Boolean)this["IsDebugEnabled"];
  15:         }
  16:         set
  17:         {
  18:             this["IsDebugEnabled"] = value;
  19:         }
  20:     }
  21: }

Hopefully this will save someone time trying to figure this out.

If you are wondering “why” this is the case, it is because the ConfigurationSection class the custom section inherits from is an abstract base class that doesn’t have a default constructor therefore one has to be added. 

posted @ Monday, September 15, 2008 10:14 AM

Print

Comments on this entry:

# re: Custom ConfigurationSection Error: Unable to load type

Left by Denis at 1/25/2009 10:22 AM
Gravatar

I bet my head in a wall for an hour. Thank you very much

Comments have been closed on this topic.
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910