访问在web.config中的一个configSection属性?属性、web、config、configSection

2023-09-07 08:43:59 作者:come back.(回头)

解决方法:我想补充的解决方案:sectionGroups似乎并不具有属性。正确的方法似乎有一个配置节作为家长和的ConfigurationElement ,因为每个孩子。还有 ConfigurationElementCollection 的集合。 .NET Framework中的一个例子:< roleManager> 是一个科<供应商> 是一个ElementCollection。我博客上讲述我的解决办法。

原题:我在web.config中的自定义sectionGroup:

 < sectionGroup名=对myAppTYPE =MyApp.MyAppSectionGroup>
  <节名称=localeSettings
           TYPE =MyApp.MyAppLocaleSettingsSection/>
< / sectionGroup>
 

该sectionGroup本身应该有一个属性:

 <对myApp defaultModule =MyApp.MyAppTestNinjectModule>
  < localeSettings longDateFormat =MM / DD / YYYY HH:MM:SS/>
< /对myApp>
 

我在访问该属性(defaultModule)的麻​​烦。使用获取的一段是很容易的 ConfigurationManager.GetSection(对myApp / localeSettings),并将其转换为一类,它继承自ConfigurationSection。

不过,我似乎无法轻易访问sectionGroup,如 ConfigurationManager.GetSection(对myApp)返回null。我试过了ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["myApp"],但是这并没有实现一个索引,让我获得 defaultModule

我误解的东西吗?是sectionGroups真的只有自己没有设置容器?我可以窝节而无需sectionGroup呢?而且是 OpenExeConfiguration 适用于.NET应用程序(使用的web.config),或者是它只是app.config中的?

编辑:有关WebConfigurationManager代替ConfigurationManager中感谢您的提示。这并没有解决我的主要问题,但至少该有更明智的命名OpenXXX方法。

目前,这些都是两班。该LocaleSettings工作完全正常,只是SectionHandler不让我进入了defaultModule属性。

 公共类MyAppSectionGroup:ConfigurationSectionGroup
{
    [的ConfigurationProperty(localeSettings)]
    公共MyAppLocaleSettingsSection LocaleSettings
    {
        得到
        {
            返回段[localeSettings]作为MyAppLocaleSettingsSection;
        }
    }
}

公共类MyAppLocaleSettingsSection:配置节
{
    [的ConfigurationProperty(longDateFormat,默认值=YYYY-MM-DD HH:MM)]
    公共字符串LongDateFormat
    {
        得到
        {
            返回此[longDateFormat]作为串;
        }
    }
}
 
怎么建web.config文件

解决方案

几乎所有你要做的应该是可能的,应该是确定和法律 - 你必须失去小东西,我猜。我不知道的唯一的事是部分组是否可以有自己的属性 - 它们可能会被设计为正在只是容器的部分,然后有实际的配置数据,他们...

有关访问的web.config文件,你也应该尝试使用的 WebConfigurationManager 而不是直线ConfigurationManager中(这是对的app.config文件)。

您可以向我们展示了code为你的 MyApp.MyAppSectionHandler MyApp.MyAppLocaleSettingsConfigurationSection

你检查出乔恩Rista的三部系列.NET 2.0的配置上$ C $的CProject?这是一个很好的介绍如何使用和扩展.NET配置系统 - 强烈推荐,最实用确实

揭开.NET 2.0配置 解码.NET 2.0配置 开裂的.NET 2.0配置

如果你正在处理自定义配置部分,我也建议你看看在配置节设计师,一个Visual Studio插件,可以让你直观地定义你的配置节组和配置部分和属性及其数据类型中的这些部分 - 节省很多时间和教育工具

一些更多的挖掘已经表明:

您可能会定义 [的ConfigurationProperty] 在自定义配置节组,但没有自动的后备存储对于那些性能好,我看不到任何挂钩到从配置文件中的XML的加载方式,要么 - 所以我想在部分组还真是只有容器 您可以嵌套部分彼此组内,但叶级别必须是配置部分,而那些没有被嵌套在任何东西,但一个配置节组可以。

马克·

Solution: Just to add the solution: sectionGroups do not seem to have attributes. The proper way seems to have a ConfigurationSection as the parent and ConfigurationElement as each children. There is also ConfigurationElementCollection for Collections. An example from the .net Framework: <roleManager> is a Section, <providers> is an ElementCollection. I blogged about my solution.

Original Question: I have a custom sectionGroup in my web.config:

<sectionGroup name="myApp" type="MyApp.MyAppSectionGroup">
  <section name="localeSettings" 
           type="MyApp.MyAppLocaleSettingsSection"/>
</sectionGroup>

The sectionGroup itself is supposed to have an attribute:

<myApp defaultModule="MyApp.MyAppTestNinjectModule">
  <localeSettings longDateFormat="MM/dd/yyyy HH:mm:ss" />
</myApp>

I have trouble accessing that attribute (defaultModule). Getting a section is very easy using ConfigurationManager.GetSection("myApp/localeSettings") and casting it to a class that inherits from ConfigurationSection.

But I can't seem to easily access the sectionGroup, as ConfigurationManager.GetSection("myApp") returns null. I've tried ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["myApp"], but this does not implement an indexer that gives me access to defaultModule.

Am I misunderstanding something? Are sectionGroups really only Containers with no settings of their own? Can I nest sections without having a sectionGroup at all? And is OpenExeConfiguration appropriate for .net applications (with web.config) or is it just for app.config's?

Edit: Thanks for the hint regarding WebConfigurationManager instead of ConfigurationManager. That doesn't solve my main problem, but at least this has more sensible named OpenXXX methods.

At the moment, these are the two classes. The LocaleSettings works perfectly fine, just the SectionHandler doesn't let my access the "defaultModule" attribute.

public class MyAppSectionGroup: ConfigurationSectionGroup
{
    [ConfigurationProperty("localeSettings")]
    public MyAppLocaleSettingsSection LocaleSettings
    {
        get
        {
            return Sections["localeSettings"] as MyAppLocaleSettingsSection;
        }
    }
}

public class MyAppLocaleSettingsSection: ConfigurationSection
{
    [ConfigurationProperty("longDateFormat", DefaultValue = "yyyy-MM-dd HH:mm")]
    public string LongDateFormat
    {
        get
        {
            return this["longDateFormat"] as string;
        }
    }
}

解决方案

Almost all you're trying to do should be possible and should be ok and legal - you must be missing something small, I guess. The only thing I'm not sure of is whether section groups can have attributes of their own - they might be designed as being just containers for sections, which then have the actual config data in them...

For accessing the web.config, you should also try to use the WebConfigurationManager instead of the "straight" ConfigurationManager (that's for app.config files).

Can you show us the code for your MyApp.MyAppSectionHandler and MyApp.MyAppLocaleSettingsConfigurationSection ?

Have you checked out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject? It's an excellent intro to how to use and extend the .NET config system - highly recommended, and most useful indeed!

Unraveling the mysteries of .NET 2.0 configuration Decoding the mysteries of .NET 2.0 configuration Cracking the mysteries of .NET 2.0 configuration

If you're dealing with custom config sections, I would also recommend you have a look at the Configuration Section Designer, a Visual Studio plug-in that allows you to visually define your config section groups and config sections and the attributes and their datatypes in those sections - great time saver and educational tool!

Some more digging has shown:

you could potentially define [ConfigurationProperty] on a custom config section group, but there is no automatic "backing store" for those properties, and I can't see any way of hooking into the loading of the XML from the config file, either - so I guess the section groups really are only containers you can nest section groups within one another, but the leaf level has to be a configuration section, and those cannot be nested in anything but a config section group.

Marc