Steve Michelotti

A .NET Developer's Toolbox

  Home  |   Contact  |   Syndication    |   Login
  175 Posts | 0 Stories | 939 Comments | 51 Trackbacks

News

View Steve Michelotti's profile on LinkedIn

profile for Steve Michelotti at Stack Overflow, Q&A for professional and enthusiast programmers




Google My Blog

What I'm Reading:

Shelfari: Book reviews on your book blog

Tag Cloud


Archives

Post Categories

Code

Publications

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;
}

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
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

# re: ConfigurationErrorsException - The configuration is read only. 9/4/2008 9:29 PM Kristaian
Thanks for this tip. Is it just me, or is this part of the .net framework really hard to get working?

How do you actually save the configuration changes back to the .config file?

# re: ConfigurationErrorsException - The configuration is read only. 9/24/2008 10:03 PM Steve
Kristaian - Check out the Save() method of the Configuration class: http://msdn.microsoft.com/en-us/library/system.configuration.configuration.save.aspx

# re: ConfigurationErrorsException - The configuration is read only. 1/7/2009 5:35 PM DevLindel
I am still getting error - "Configuration is read only" even after i have overididden the Isreadonly function. Any ideas?


Imports Microsoft.VisualBasic
Imports System.Configuration

Public Class ConfigsectionOverride
Inherits ConfigurationElement

Overrides Function IsReadOnly() As Boolean
Return False
End Function

Public Sub setConnection()

Dim connectString As New ConnectionStringSettings
ConfigurationManager.ConnectionStrings.RemoveAt(0)
connectString.Name = "PRSConnectionString"
connectString.ConnectionString = "User ID=sa;Password=password;Initial Catalog=TestDB;Data Source=TestServer"
ConfigurationManager.ConnectionStrings.IsReadOnly()
End Sub

Public Sub New()
IsReadOnly()
End Sub
End Class

# re: ConfigurationErrorsException - The configuration is read only. 1/8/2009 9:22 AM Steve
DevLindel - I'm not totally sure what you're trying to do above. is the setConnection() method part of your ConfigsectionOverride class? You're checking the readonly property of the ConnectionStrings section which is different from your section. Seems to be applies and oranges.

# re: ConfigurationErrorsException - The configuration is read only. 1/8/2009 10:56 AM DevLindel
Sorry for confusion -

I have wrttien a class ConfigsectionOverride to override ConfigurationElement.IsReadOnly.
setConnection() is written to add a connection string in connection string collection of web.config file.

Everytime when setconnection() get called ConfigurationManager.ConnectionStrings.RemoveAt(0) it throws an exception for "Configuration is read only". To overcome it i have overridden Isreadonly() to return false as described on first post here.

I am using Enterprise Libraries for Data access. My motive is to set the value of connection string variable "PRSConnectionString" dynamically based on environment it is intalled on (say Live, Testing, Development etc). I should be able to do this by changing the value of "PRSConnectionString" on application start of global.asax of my ASP.NET/VB.NET 2.0 web application.

Regards,

Dev

# re: ConfigurationErrorsException - The configuration is read only. 1/8/2009 11:22 AM Steve
It seems you are not using the WebConfigurationManager.OpenWebConfiguration() method. Have a look at these links:

http://msdn.microsoft.com/en-us/library/ms151456.aspx

http://www.beansoftware.com/ASP.NET-Tutorials/Modify-Web.Config-Run-Time.aspx


# re: ConfigurationErrorsException - The configuration is read only. 3/27/2009 9:08 AM Jigar Patel
Great ....

I am looking for a solution of one problem....

I came across this article post...

and guess what!!!!

it solves my two problem....

Cheers...

Thanks a lot buddy...

# re: ConfigurationErrorsException - The configuration is read only. 9/10/2009 4:00 AM Femi Ojemuyiwa
This is great stuff errrrr?????? any examples? I dont wanna write a whole class just change MaxRequestLength at runtime?

# re: ConfigurationErrorsException - The configuration is read only. 9/10/2009 7:31 AM Steve
@Femi Ojemuyiwa - I guess it depends what you're trying to do. The above example is specifically for scenarios where you have already implemented the class for your custom configuration section.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: