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;  
Tweet Post Share Update Email RSS