Trying to create a custom configuration elements as seen below:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ScriptObjects" type="ScriptObjectSection" />
</configSections>
<ScriptObjects>
<users>
<user name="Justin"/>
</users>
</ScriptObjects>
</configuration>
But I’m getting this error:
An error occurred creating the configuration section handler for
ScriptObjects: Could not load type ‘ScriptObjectSection’ from assembly
‘System.Configuration, Version=4.0.0.0, Culture=neutral
I want to have the ability of looping through all user elements.
I’ve searched the net and found a sample from MSDN, but its a slightly too cryptic for me to understand.
Below is the code I have for the custom element, element collection, and config section.
Does any one have any guidance on this? Where am I messing up?
public class ScriptObjectSection : ConfigurationSection
{
[ConfigurationProperty("Users")]
[ConfigurationCollection(typeof(UserCollection))]
public UserCollection Users
{
get { return (UserCollection)base[ "users" ]; }
}
}
public class UserElement : ConfigurationElement
{
[ConfigurationProperty( "name" )]
public string Name
{
get { return (string)this[ "name" ]; }
}
}
public class UserCollection : ConfigurationElementCollection
{
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.AddRemoveClearMap;
}
}
protected override ConfigurationElement CreateNewElement()
{
throw new NotImplementedException();
}
protected override object GetElementKey( ConfigurationElement element )
{
throw new NotImplementedException();
}
public UserElement this[ int index ]
{
get { return (UserElement)BaseGet( index ); }
}
}
The Error is thrown on this line:
ScriptObjectSection sos = ConfigurationManager.GetSection("ScriptObjects") as ScriptObjectSection;
You should check out Jon Rista’s three-part series on .NET 2.0 configuration up on CodeProject.
Highly recommended, well written and extremely helpful! I learned a lot from those articles and was able to figure out my config needs from studying those code snippets.
Looking at your code, the most probable cause is that you need to fully qualify your config section in the config section definition:
The
type=attribute needs to be in the form ofnamespace.ClassName,assemblyName