引用项目中的 ASP.NET 自定义配置部分不起作用?
我创建了一个继承自 System.Configuration.ConfigurationSection 的自定义类,其实现如下(这显然只是为了让真正的配置运行):
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"];
}
}
}
完整的命名空间 + 类的名称是 MyApp。 Core.Config,它位于 MyApp.Core 项目中,该项目编译为 MyApp.Core.dll 程序集。然后,我从我的 ASP.NET/MVC3 项目(称为 MyApp.UI)引用了该项目。我已经编辑了 Web.config
文件以添加 sectionGroup
和 section
元素,如下所示:
<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>
但是,它看起来不像类型MyApp.Core.Config
用于验证配置或在编辑配置时提供智能感知。以下是我的一些观察结果:
- 我将
yourAge
属性定义为IsRequired = true
,并且应用程序在没有它的情况下也可以正常运行。 - 我什至可以更改
元素中的类型(更改为 Foo.Bar 等根本不存在的类型),并且一切仍然运行正常
- 但是如果我删除
< ;section>
元素,应用程序无法运行。
我很困惑,有什么帮助吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能没有使用该配置?
如果您不使用它,它只会检查根节点是否通过 configSections 注册,但不会查看此时的详细信息。
当您实际使用
ConfigurationManager.GetSection
加载它时,将验证设置,并且在您的配置中,它将触发一个异常,告知缺少所需的YourAge
属性。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 requiredYourAge
property is missing.