简体   繁体   中英

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

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!)

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

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

//Do work

config.Save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM