Posts
208
Comments
1144
Trackbacks
51
ConfigurationErrorsException - The configuration is read only.

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 Print
Comments
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
Sam Piper
9/19/2007 1:50 AM
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.
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
Bryan
10/11/2007 2:18 AM
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.
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
Mihai
3/30/2008 10:26 AM
Thanks a lot for this tip. It drove me crazy!
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
Noshirwan Sheriyarji
8/26/2008 12:15 PM
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
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
Kristaian
9/4/2008 9:29 PM
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?
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
Steve
9/24/2008 10:03 PM
Kristaian - Check out the Save() method of the Configuration class: http://msdn.microsoft.com/en-us/library/system.configuration.configuration.save.aspx
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
DevLindel
1/7/2009 5:35 PM
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
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
Steve
1/8/2009 9:22 AM
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.
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
DevLindel
1/8/2009 10:56 AM
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
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
Steve
1/8/2009 11:22 AM
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
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
Jigar Patel
3/27/2009 9:08 AM
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...
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
Femi Ojemuyiwa
9/10/2009 4:00 AM
This is great stuff errrrr?????? any examples? I dont wanna write a whole class just change MaxRequestLength at runtime?
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
Steve
9/10/2009 7:31 AM
@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.
Gravatar
# re: ConfigurationErrorsException - The configuration is read only.
SM
3/16/2013 8:54 AM
I have added the above code in my configurationelement class but the error remains. I need to write custom section in web.config dynamically.

Post Comment

Title *
Name *
Email
Comment *  
Verification

View Steve Michelotti's profile on LinkedIn

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




Google My Blog

Tag Cloud