.NET ConfigurationElementCollection.Add method does not add item to collection

1k views Asked by At

I have a custom configuration section running under ASP.NET 4.0 that I would like to save changes to. The classes involved are:

  • QueueConfiguration: a ConfigurationElement
    • QueueCollection Queues // a list of queues to be maintained
  • QueueCollection: a ConfigurationElementCollection
  • Queue: a ConfigurationElement

This works:

// This is a test case to ensure the basics work
Queue newQueue = new Queue();
QueueCollection queues = new QueueCollection();
queues.Add(newQueue); // queues.Count is incremented here

This does not work (and does not throw an error):

// Read existing QueueConfiguration
Queue newQueue = new Queue();
QueueConfiguration queueConfig = ConfigurationManager.GetSection("Queuing") as QueueConfiguration;
queueConfig.Queues.Add(newQueue); // queues.Count is NOT incremented here

This also does not work (and does not throw an error):

// Load QueueConfiguration from web.config
Queue newQueue = new Queue();
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
QueueConfiguration queueConfig = config.GetSection("Queuing") as QueueConfiguration;
queueConfig.Queues.Add(newQueue); // queues.Count is NOT incremented here    

My first through was that the one of the three configuration sections was marked as read-only. However, when I debug, the IsReadOnly() method returns false for my QueueConfiguration and my QueueCollection instances.

My QueueCollection class include this snippet:

public void Add(Queue queue)
{
  base.BaseAdd(queue, true);
}

When I step through the code, this line is reached, base.BaseAdd is called, but the Count property is not incremented. It's as if BaseAdd simply ignores the request.

Any suggestions? (I hope I'm missing something obvious!)

1

There are 1 answers

1
gsirianni On

Did you call Config.Save() after adding the times to your queue.

Configuration config = WebConfigurationManager.OpenWebConfiguration("~");

//Do work

config.Save();