C # - ConfigurationSection isRequired attribute

I have this strange problem ... in my code, if I set the IsRequired value to false or true, then it remains false. However, if I entered DefaultValue, does it work?

Inoperative code:

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 working code:

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

Why do I need to set DefaultValue to ""?

+7
c # config configuration
source share
4 answers

I ran into the same problem and found a solution here http://msdn.microsoft.com/en-us/library/system.configuration.configurationpropertyattribute%28v=vs.90%29.aspx#1 . The comment on ConfigurationPropertyAttribute not entirely correct, but it explains the basics of the problem:

The IsRequired ConfigurationPropertyAttribute element IsRequired not work if applied to a child object (obtained from ConfigurationElement ). When the subsystem reflects 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 save it in the list of parent values. Later, when he checks whether all the necessary properties have been specified or not, he cannot distinguish between the default initialized child and the one that is explicitly contained in the configuration file.

The most optimal solution would be to programmatically declare the necessary elements through the ConfigurationProperty class. This requires much more work than a declarative approach. Alternative...

As far as I can tell, the alternative is wrong.

An example software model can be found on the ConfigurationProperty page. I managed to solve the problem for myself by declaring the properties I needed in the constructor of my custom element and leaving everything else the same.

I suspect that it actually does not work when you add DefaultValue , but rather throw an exception for another reason. You will need to expand to the end of the InnerException chain to find ConfigurationErrorsException . The correct message when the required property is missing is โ€œThe required host attribute was not found. (C: \ path \ to \ yourproject \ bin \ Debug \ yourproject.vshost.exe.Config line ##)"

When you specify an empty default value for a string, the configuration subsystem will try to HostElement that string in HostElement and fail. As a result, a ConfigurationErrorsException has the message "The default value for the" host "property cannot be parsed. Error: The object link is not set to the object instance. (C: \ path \ to \ yourproject \ bin \ Debug \ yourproject.vshost.exe .Config line ##) "

+6
source share

Sorry for the necroposting, but this problem hit me too, but in a weirder way, and my solution also relates to the question asked.

I implemented a reload of the configuration without restarting the process. When the process begins, the IsRequired attribute is ignored, and the ConfigurationElement is initialized with default initialization with default values. But when reloading the configuration, the IsRequired attribute is respected ! Therefore, I hard-coded reloading the configuration when the process started and solved the problem of the absence of an exception!

pseudo code:

 config = (SampleConfiguration)ConfigurationManager.GetSection(ConfigSectionName); // <-- no exception thrown for missing required properties ConfigurationManager.RefreshSection(ConfigSectionName); config = (SampleConfiguration)ConfigurationManager.GetSection(ConfigSectionName); // <-- exception thrown! 
+3
source share

Digging a dead thread. But I accidentally found a job for this.

In your custom section constructor, make a reference to the custom ElementInformation element. Thus, another instance of your custom section will be created in the context of the element. And for some reason, which I do not fully understand, the IsRequired property is satisfied.

 public class FtpSettingsSection : ConfigurationSection { public FtpSettingsSection() { // force it to double load. if (this.Host.ElementInformation.IsPresent) ; } [ConfigurationProperty("host", IsRequired = true)] public HostElement Host { get { return (HostElement)this["host"]; } set { this["host"] = value; } } } 
+2
source share

I assume that you do not have the URL value serialized in your configuration. Therefore, when the configuration is loaded, the ConfigurationManager checks the attributes to see if a property value is required, and then throws an exception if it does not find the value. If the default value is set, then this value is used if it is not found in the configuration.

0
source share

All Articles