I'm trying to implement a custom configuration section so that I can load a list of user defined items and not having much luck. I've read through the following posts, but I still can't figure out what it is that I'm doing wrong. They seem to be good guides, but I'm missing some important fact here. I'm hoping someone can point out exactly what.
- How to implement a ConfigurationSection with a ConfigurationElementCollection
- Unit Testing custom ConfigurationElement & ConfigurationElementCollection
- Custom app.config section with a simple list of “add” elements
This is my test. When I step through it, config
remains null. It's like the call to GetSection
does nothing at all.
[TestClass]
public class ToDoConfigTests
{
[TestMethod]
public void TestGetTodoAttribute()
{
var config = ConfigurationManager.GetSection("ToDoListAttributesSection") as ToDoItemsConfigurationSection;
Assert.Fail();
}
}
My Configuration Classes:
using System.Configuration;
using Rubberduck.ToDoItems;
namespace Rubberduck.Config
{
public class ToDoItemsConfigurationCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new ToDoListAttributeElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ToDoListAttributeElement)element).Comment;
}
}
public class ToDoItemsConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("ToDoListAttributes", IsRequired = true, IsDefaultCollection=true)]
public ToDoItemsConfigurationCollection ToDoListAttributes
{
get { return (ToDoItemsConfigurationCollection)this["ToDoListAttributes"]; }
set { this["ToDoListAttributes"] = value; }
}
}
public class ToDoListAttributeElement : ConfigurationElement
{
[ConfigurationProperty("TaskPriority", DefaultValue = TaskPriority.Low, IsRequired = true)]
public TaskPriority Priority
{
get { return (TaskPriority)this["TaskPriority"]; }
set { this["TaskPriority"] = value; }
}
[ConfigurationProperty("Comment",IsKey=true, IsRequired = true)]
public string Comment
{
get { return (string)this["Comment"]; }
set { this["Comment"] = value; }
}
}
}
Finally, the app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Rubberduck.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
<section name="ToDoListAttributesSection" type="Rubberduck.Config.ToDoItemsConfigurationSection, Rubberduck.Config"/>
</configSections>
<ToDoListAttributesSection>
<ToDoListAttributes>
<add Comment="note" TaskPriority="0" />
<add Comment="todo" TaskPriority="1" />
<add Comment="bug" TaskPriority="2"/>
</ToDoListAttributes>
</ToDoListAttributesSection>
</configuration>