ASP.NET Custom ConfigurationSection from referenced project not working?
I've created a custom class that inherits from System.Configuration.ConfigurationSection
, implemented like this (this is obviously just to get the real configuration running):
public class Config : ConfigurationSection
{
[ConfigurationProperty("quoteOfTheDay", DefaultValue = "It is what it is.", IsRequired = false)]
public string QuoteOfTheDay
{
get
{
return this["quoteOfTheDay"] as string;
}
}
[ConfigurationProperty("yourAge", IsRequired = true)]
public int YourAge
{
get
{
return (int)this["yourAge"];
}
}
}
The full namespace + name of the class is MyApp.Core.Config, it's located in the MyApp.Core project which compiles to the MyApp.Core.dll assembly. I've then referenced this project from my ASP.NET/MVC3 project, which is called MyApp.UI. I've edited the Web.config
file to add the sectionGroup
and section
elements like this:
<configSections>
开发者_开发百科<sectionGroup name="myAppConfigGroup">
<section name="myAppSection"
type="MyApp.Core.Config, MyApp.Core, Version=1.0.0.0, Culture=neutral, PublicKey=null"
allowLocation="true"
allowDefinition="Everywhere" />
</sectionGroup>
</configSections>
<myAppConfigGroup>
<myAppSection>
</myAppSection>
</myAppConfigGroup>
However, it doesn't seem like the type MyApp.Core.Config
get used to validate the configuration or provide intelli-sense as I edit the configuration. Here's some observations I've made:
- I defined the
yourAge
property asIsRequired = true
and the application runs fine without it. - I can even change the type (to some type that doesn't even exist like Foo.Bar) in the
<section>
element, and everything still runs fine - But if I remove the
<section>
element, the app doesn't run.
I'm baffled, any help?
You are probably not using the configuration?
If you are not using it, it will only do a check if the root nodes are registered via configSections
but it will not look at the details at that point.
When you will actually load it using ConfigurationManager.GetSection
the settings will be validated and in your case of this configuration it will trigger an exception telling that the required YourAge
property is missing.
精彩评论