ConfigurationPropertyAttribute.IsRequired Property

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I was creating a Custom Configuration section in my web.config.

I noticed that if I set a property like:
[ConfigurationProperty("EmailAuthorizationFailures", IsRequired = true)]

<MySection
EmailAuthorizationFailures="true"
/>


and I neglected to put the entire MySection in the web.config. I don't get
an error. Additionally if I actually read the section using
GetConfig("MySection") it comes back full of values (DefaultValue or
ValueType defaults ). I thought it would be NULL.

I would like to throw an exception if the user forgets to put in the
section in the web.config.
Is their a way to test for the missing section?

thanks,
 
Hi Chuck,

As for the "how to test the missing of a configuration section in .net
config file", I think it a good question. However, based on my research,
the current .NET configuration settings model doesn't provide support of
detecting the existence of a configuration section automatically(only the
detection of a sub section properrty is supported). This limitation is due
to the following design of the configuration section processing mechanism:

** When .net application runs, it will not always load all the
configuration settings at one time or at the startup time. The loading of
each configuration section is on-demand and may occur at different time.
Thus, it won't be convenient to check all the potential configsection's
existence.

** So far <configSections><section> element only support the definition of
the scope and modify-behavior of the registered section, haven't provided
setting to control the existence detection

For your scenario, if you do need to take care of some certain custom
configuration's existence, I think you may consider manually use the
configuration API to query the certain section in code or use XML API to
perform xml query against the app.config file.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks,
I'll think I will use a little hack.
Create a setting. IhaveBeenSpecified default =false.
When I read the settings in code if it is false, I'll throw an exception.
 
Back
Top