Can't make simple ConfigurationSection object work

  • Thread starter Thread starter Brad Wood
  • Start date Start date
B

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).
 
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 said:
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).
 
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

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 said:
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).
 
Dmytro said:
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.
 
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
 
Back
Top