2

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

Eric Patrick
  • 2,097
  • 2
  • 20
  • 31
  • Are you trying to edit the configuration file live within your site? – Brian Mains Dec 08 '11 at 18:35
  • That's my ultimate goal, and I successfully do that with other configuration sections. However, without the ability to add a Queue to my QueueCollection prior to the save, the save is pointless. – Eric Patrick Dec 08 '11 at 19:58

1 Answers1

0

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

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

//Do work

config.Save();
gsirianni
  • 1,334
  • 2
  • 18
  • 35
  • Yes, I did. The configuration is persisted to the .config file without the Queue I tried to add to the collection. I know the .config file is changed, as the formatting of the XML is altered by the config.Save() method. I think, however, that reaching config.Save() is irrelevant to my problem. If calling Queues.Add() does not in fact add a Queue (or throw an error), saving the config is moot. – Eric Patrick Dec 21 '11 at 16:26