I'm having this wierd problem... in my code whether I set the value of IsRequired to false or true then it stays false.. However if I put in a DefaultValue it works?
The non-working code is:
public class FtpSettingsSection : ConfigurationSection
{
[ConfigurationProperty("host", IsRequired = true)]
public HostElement Host
{
get { return (HostElement)this["host"]; }
set { this["host"] = value; }
}
}
public class HostElement : ConfigurationElement
{
[ConfigurationProperty("URL", IsRequired = true)]
public string URL
{
get { return (string)this["URL"]; }
set { this["URL"] = value; }
}
}
and the working code is:
public class FtpSettingsSection : ConfigurationSection
{
[ConfigurationProperty("host", DefaultValue = "", IsRequired = true)]
public HostElement Host
{
get { return (HostElement)this["host"]; }
set { this["host"] = value; }
}
}
public class HostElement : ConfigurationElement
{
[ConfigurationProperty("URL", DefaultValue = "", IsRequired = true)]
public string URL
{
get { return (string)this["URL"]; }
set { this["URL"] = value; }
}
}
How come that I need to set DefaultValue to ""?
I have encountered the same problem and found the solution here http://msdn.microsoft.com/en-us/library/system.configuration.configurationpropertyattribute%28v=vs.90%29.aspx#1. The comment on ConfigurationPropertyAttribute
isn't completely correct, but it explains the basics of the problem:
The
IsRequired
member ofConfigurationPropertyAttribute
does not work when applied to a child object (deriving fromConfigurationElement
). When the subsystem reflects over the attributes of the parent section/element to determine which configuration properties are defined it will create a new instance (of the appropriate type) for each child element and store it in the parent's value list. Later, when it validates whether all required properties have been specified or not, it cannot differentiate between a default initialized child element and one that was explicitly contained in the configuration file.The most ideal workaround would be to programmatically declare the required elements through the
ConfigurationProperty
class. This requires substantial more work than the declarative approach. An alternative...
As far as I can tell the alternative is incorrect.
An example of the programmatic model can be found on the ConfigurationProperty
page. I have managed to fix the problem for myself by declaring the properties I need required in the constructor of my custom element and leaving everything else the same.
I suspect for you it is not actually working when you add DefaultValue
, but rather throwing an exception for a different reason. You will have to drill down to the end of the InnerException
chain to find the ConfigurationErrorsException
. The correct message when a required property is missing is "Required attribute 'host' not found. (C:\path\to\yourproject\bin\Debug\yourproject.vshost.exe.Config line ##)"
When you provide an empty string default value the configuration subsystem will try to parse that string into a HostElement
and fail. The resulting ConfigurationErrorsException
has the message "The default value of the property 'host' cannot be parsed. The error is: Object reference not set to an instance of an object. (C:\path\to\yourproject\bin\Debug\yourproject.vshost.exe.Config line ##)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With