Home All Groups Group Topic Archive Search About

Can't make simple ConfigurationSection object work

Author
9 Feb 2006 11:17 PM
Brad Wood
Here is perhaps the simplest possible ConfigurationSection sample along
with it's App.Config:

======================================================
using System;
using System.Configuration;

namespace ConsoleApplication1
{

   class Program
   {

     public class BoboSection : ConfigurationSection
     {
       [ConfigurationProperty("mom", IsRequired = true)]
       public string mom
       {
         get { return (string)base["mom"]; }
         set { base["mom"] = value; }
       }
     }

     static void Main( string[] args )
     {
       BoboSection bobo =
(BoboSection)ConfigurationManager.GetSection("Bobo");
       Console.WriteLine( bobo.mom );
       Console.ReadLine();
     }

   }
}
======================================================
<configuration>
   <configSections>
     <section name="Bobo" type="BoboSection, ConsoleApplication1" />
   </configSections>
   <Bobo mom="hi mom" />
</configuration>
======================================================

When I run this, I get the error, "An error occurred creating the
configuration section handler for Bobo: Could not load type
'BoboSection' from assembly 'ConsoleApplication1'".
The path to ConsoleApplication1.exe is shown correctly.  It makes no
difference if I change the section element's type attribute to
"ConsoleApplication1.BoboSection, ConsoleApplication1" or if I give the
full name of the assembly (ConsoleApplication1, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null).

Author
10 Feb 2006 11:18 AM
Dmytro Lapshyn [MVP]
Hi Brad,

First of all, I wouldn't make the config section class a nested class, just
to avoid unnecessary hassle with type naming. Second, mind the *full* type
name in the tag below:

> <section name="Bobo" type="BoboSection, ConsoleApplication1" />

What you need with your current namespace/class hierarchy is something like

<section name="Bobo" type="ConsoleApplication1.Program.BoboSection,
ConsoleApplication1" />

But I am not sure what the naming convention is for nested classes.

Show quote
"Brad Wood" <bradley|.wood|@ndsu|.edu> wrote in message
news:O5lHy7cLGHA.2124@TK2MSFTNGP14.phx.gbl...
> Here is perhaps the simplest possible ConfigurationSection sample along
> with it's App.Config:
>
> ======================================================
> using System;
> using System.Configuration;
>
> namespace ConsoleApplication1
> {
>
>   class Program
>   {
>
>     public class BoboSection : ConfigurationSection
>     {
>       [ConfigurationProperty("mom", IsRequired = true)]
>       public string mom
>       {
>         get { return (string)base["mom"]; }
>         set { base["mom"] = value; }
>       }
>     }
>
>     static void Main( string[] args )
>     {
>       BoboSection bobo =
> (BoboSection)ConfigurationManager.GetSection("Bobo");
>       Console.WriteLine( bobo.mom );
>       Console.ReadLine();
>     }
>
>   }
> }
> ======================================================
> <configuration>
>   <configSections>
>     <section name="Bobo" type="BoboSection, ConsoleApplication1" />
>   </configSections>
>   <Bobo mom="hi mom" />
> </configuration>
> ======================================================
>
> When I run this, I get the error, "An error occurred creating the
> configuration section handler for Bobo: Could not load type 'BoboSection'
> from assembly 'ConsoleApplication1'".
> The path to ConsoleApplication1.exe is shown correctly.  It makes no
> difference if I change the section element's type attribute to
> "ConsoleApplication1.BoboSection, ConsoleApplication1" or if I give the
> full name of the assembly (ConsoleApplication1, Version=1.0.0.0,
> Culture=neutral, PublicKeyToken=null).
Author
10 Feb 2006 2:02 PM
Matt Sollars
Brad,

I agree with Dmytro. You'll likely want the section class to itself in
the namespace instead of nested. However, his suggestion for making it
work with your setup looks accurate to me
([Namespace].[ParentClass].[NestedClass]).

Good luck!

Matt


Dmytro Lapshyn [MVP] wrote:
Show quote
> Hi Brad,
>
> First of all, I wouldn't make the config section class a nested class, just
> to avoid unnecessary hassle with type naming. Second, mind the *full* type
> name in the tag below:
>
>> <section name="Bobo" type="BoboSection, ConsoleApplication1" />
>
> What you need with your current namespace/class hierarchy is something like
>
> <section name="Bobo" type="ConsoleApplication1.Program.BoboSection,
> ConsoleApplication1" />
>
> But I am not sure what the naming convention is for nested classes.
>
> "Brad Wood" <bradley|.wood|@ndsu|.edu> wrote in message
> news:O5lHy7cLGHA.2124@TK2MSFTNGP14.phx.gbl...
>> Here is perhaps the simplest possible ConfigurationSection sample along
>> with it's App.Config:
>>
>> ======================================================
>> using System;
>> using System.Configuration;
>>
>> namespace ConsoleApplication1
>> {
>>
>>   class Program
>>   {
>>
>>     public class BoboSection : ConfigurationSection
>>     {
>>       [ConfigurationProperty("mom", IsRequired = true)]
>>       public string mom
>>       {
>>         get { return (string)base["mom"]; }
>>         set { base["mom"] = value; }
>>       }
>>     }
>>
>>     static void Main( string[] args )
>>     {
>>       BoboSection bobo =
>> (BoboSection)ConfigurationManager.GetSection("Bobo");
>>       Console.WriteLine( bobo.mom );
>>       Console.ReadLine();
>>     }
>>
>>   }
>> }
>> ======================================================
>> <configuration>
>>   <configSections>
>>     <section name="Bobo" type="BoboSection, ConsoleApplication1" />
>>   </configSections>
>>   <Bobo mom="hi mom" />
>> </configuration>
>> ======================================================
>>
>> When I run this, I get the error, "An error occurred creating the
>> configuration section handler for Bobo: Could not load type 'BoboSection'
>> from assembly 'ConsoleApplication1'".
>> The path to ConsoleApplication1.exe is shown correctly.  It makes no
>> difference if I change the section element's type attribute to
>> "ConsoleApplication1.BoboSection, ConsoleApplication1" or if I give the
>> full name of the assembly (ConsoleApplication1, Version=1.0.0.0,
>> Culture=neutral, PublicKeyToken=null).
>
>
Author
10 Feb 2006 3:20 PM
Brad Wood
Dmytro Lapshyn [MVP] wrote:
> Hi Brad,
>
> First of all, I wouldn't make the config section class a nested class

Man, do I feel stupid.  That's all it was; I accidentally made the class
nested and was blind to that fact after doing so.

Thanks.
Author
14 Feb 2006 4:44 PM
Matt Sollars
Brad,

If you have the need for validation and begin working with those
declarative attributes on your configuration properties
(IntegerValidator, StringValidator, etc.), let me know how it works out
for you, please.

Everything I try fails!


Thanks,

Matt



Brad Wood wrote:
Show quote
> Dmytro Lapshyn [MVP] wrote:
>> Hi Brad,
>>
>> First of all, I wouldn't make the config section class a nested class
>
> Man, do I feel stupid.  That's all it was; I accidentally made the class
> nested and was blind to that fact after doing so.
>
> Thanks.

AddThis Social Bookmark Button