Steve Michelotti

C#, ASP.NET, and other stuff

  Home  |   Contact  |   Syndication    |   Login
  45 Posts | 1 Stories | 131 Comments | 52 Trackbacks

News



Archives

Post Categories

Image Galleries

Articles

Blogs

The .NET 2.0 Configuration API is a huge step up from the previous versions of the framework rendering many other previous configuration framework (Enterprise Library Configuration block, etc.) virtually obsolete.  However, one thing that can trip people up is when they try to assign to a configuration property at run-time you can get a ConfigurationErrorsException - The configuration is read only even when a setter is defined on their property:

[ConfigurationProperty(item1Property, DefaultValue = 1, IsRequired=true)]
[IntegerValidator(MinValue=1, MaxValue=100)]
public int ConfigItem1
{
   get  { return Convert.ToInt32(this[item1Property]); }
   set  { this[item1Property] = value; }
}

The ConfigurationSection inherits from ConfigurationElement which defines a virtual method for IsReadOnly().  If you want to avoid this exception and actually assign your values at run-time simply override this method in your ConfigurationSection or ConfigurationElement class like this:

public override bool IsReadOnly()
{
    return false;
}

posted on Tuesday, August 14, 2007 10:23 PM

Feedback

# re: ConfigurationErrorsException - The configuration is read only. 9/19/2007 1:50 AM Sam Piper
Great! Just what I was looking for.

I would just add that you need to override this method for EVERY custom section or element in your configuration graph, not just the root section.

# re: ConfigurationErrorsException - The configuration is read only. 10/11/2007 2:18 AM Bryan
Forgive me for asking.. but since ConfigurationSecion is a class from the framework class library, how do you override one of it's methods?

Do you mean cereate your own ConfigurationSecion class and have it derrived from the FCL ConfigurationSection class?

Once again, forgive me.. I'm new and I don't really grasp you work around..

This looks like it will solve a problem I'm experiencing.

Thanks for the article..

Can you help me understand?

Thanks.

# re: ConfigurationErrorsException - The configuration is read only. 3/30/2008 10:26 AM Mihai
Thanks a lot for this tip. It drove me crazy!

# re: ConfigurationErrorsException - The configuration is read only. 8/26/2008 12:15 PM Noshirwan Sheriyarji
Wow just what I had wanted. I was searching for something like this since the morning. But since my search strings on google were not related to this exception so I was getting every damn thing except this. Thanks a great deal!!!
--Nosh

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 4 and 2 and type the answer here: