ASP.NET 自定义配置节声明破坏了 IIS 管理器配置编辑器

Kev*_*Kev 5 iis iis-7 configuration iis-7.5

我有一个简单的 .NET 自定义配置组件,它允许我在我的 ASP.NET 2.0(Web 项目面向 .NET Framework 3.5)Web 应用程序的web.config文件中指定一个自定义配置组和部分:

在我的web.config我有以下声明:

<configuration>

  <configSections>
    <sectionGroup 
      name="SimpleConfigGroup"
      type="CustomSettingsLib.SimpleConfigGroup, CustomSettingsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a58d3af5d6768c9">
      <section 
        name="SimpleConfigSection"
        type="CustomSettingsLib.SimpleConfigSection, CustomSettingsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a58d3af5d6768c9"/>
    </sectionGroup>
  </configSections>

  <SimpleConfigGroup>
    <SimpleConfigSection MySetting="Hello World" />
  </SimpleConfigGroup>

</configuration>
Run Code Online (Sandbox Code Playgroud)

有几个类可以访问位于名为 的类库项目中的这个自定义配置部分CustomSettingsLib

SimpleConfigGroup.cs:

using System.Configuration;
namespace CustomSettingsLib
{
  public class SimpleConfigGroup : ConfigurationSectionGroup
  {
    [ConfigurationProperty("SimpleConfigSection")]
    public SimpleConfigSection SimpleConfigSection
    {
      get { return (SimpleConfigSection)this.Sections["SimpleConfigSection"]; }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

SimpleConfigSection.cs:

using System.Configuration;
namespace CustomSettingsLib
{
  public class SimpleConfigSection : ConfigurationSection
  {
    [ConfigurationProperty("MySetting", IsRequired = false)]
    public string MySetting
    {
      get { return (string)this["MySetting"]; }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

读取MySetting值的代码类似于 ( Default.aspx.cs):

using System;
using System.Configuration;
using System.Web.Configuration;
using CustomSettingsLib;

namespace WebApplication4
{
  public partial class Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
      SimpleConfigGroup group = 
              config.GetSectionGroup("SimpleConfigGroup") as SimpleConfigGroup;
      SimpleConfigSection section = group.SimpleConfigSection;
      Response.Write(section.MySetting);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这很好用,我的 Web 应用程序可以MySetting从我的web.config文件中读取值。

由于这些设置将在 Windows 2008 R2+IIS7.5 上的许多 Web 应用程序中使用,因此我创建了一个IIS 配置架构扩展,以允许在 IIS 管理器的配置编辑器功能中编辑此设置,而不必手动编辑web.config文件。

我添加了一个 IIS 配置架构扩展文件到:

%systemroot%\system32\inetsrv\config\schema

<configSchema>
  <sectionSchema name="SimpleConfigGroup">
    <element name="SimpleConfigSection">
      <attribute name="MySetting" 
                 type="string" 
                 validationType="nonEmptyString" />
    </element>
  </sectionSchema>
</configSchema>
Run Code Online (Sandbox Code Playgroud)

然后我将以下部分定义添加到元素applicationHost.config中的IIS文件中<configSections>

<section name="SimpleConfigGroup" 
         overrideModeDefault="Allow" 
         allowDefinition="Everywhere" />
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,当我为站点打开 IIS 管理器的配置编辑器时:

在此处输入图片说明

然后SimpleConfigGroup从部分下拉列表中选择它报告以下模糊错误:

“执行此操作时出错。”
详细信息:
文件名:\?\e:\sites\site1\web.config
错误:

这是此错误的屏幕截图:

在此处输入图片说明

如果我<sectionGroup>从站点web.config文件中删除 .NET声明,我可以使用 IIS 管理器的配置编辑器很好地编辑自定义设置。但是,我的 Web 应用程序无法运行,因为<sectionGroup>自定义配置部分和 .NET 需要配置信息才能解析web.config文件。

我已经尝试将<sectionGroup>加到根目录中machine.config(甚至将配置程序集放到 .NET 2.0 框架程序集文件夹中),但我得到了同样的错误。我还尝试签署配置程序集,认为可能存在信任问题,但这也无济于事。

我觉得奇怪的是,平时的.NET框架<configSections>machine.config不打乱IIS管理器配置管理器,也不会在.NET 3.5 System.Web.Extensions程序<sectionGroup>定义在ASP.NET 2.0的网站,例如,对于system.web还没有我的自定义配置部分做.

为什么是这样?这是 IIS 管理器配置编辑器中的错误吗?

更新:

根据 Scott 的建议,我将以下内容添加到我的applicationHost.config文件中(将sectionGroup/section声明留在web.config文件中:

<sectionGroup name="SimpleConfigGroup">
  <section name="SimpleConfigSection" 
           allowDefinition="Everywhere" 
           overrideModeDefault="Allow" />
</sectionGroup>
Run Code Online (Sandbox Code Playgroud)

这会产生以下错误,这是可以理解的,因为它已在web.config文件中定义:

在此处输入图片说明


我尝试从 中删除sectionGroup/section声明,web.config但将上述sectionGroup声明保留在applicationHost. IIS 管理器的配置编辑器不再列出该SimpleConfigGroup部分。


然后我在applicationHost.config

<section 
    name="SimpleConfigGroup" 
    allowDefinition="Everywhere" 
    overrideModeDefault="Allow" 
    type="CustomSettingsLib.SimpleConfigGroup, CustomSettingsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a58d3af5d6768c9"/>
Run Code Online (Sandbox Code Playgroud)

IIS 管理器的配置设置编辑器现在可以再次查看该部分而不会出错,但是因为web.configASP.NET 应用程序中没有部分声明会引发“无法识别的配置部分”异常。


我也在applicationHost.config

<sectionGroup 
   name="SimpleConfigGroup" 
   type="CustomSettingsLib.SimpleConfigGroup, CustomSettingsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a58d3af5d6768c9">
  <section 
    name="SimpleConfigSection" 
    overrideModeDefault="Allow" 
    allowDefinition="Everywhere"
    type="CustomSettingsLib.SimpleConfigSection, CustomSettingsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a58d3af5d6768c9"/>
</sectionGroup>
Run Code Online (Sandbox Code Playgroud)

同样,没有骰子,ASP.NET 抛出“无法识别的配置部分”异常,IIS 管理器的配置编辑器不再列出该SimpleConfigGroup部分。


进步:

我恢复到第一基础并重新设置web.config配置部分声明,以便 ASP.NET 应用程序可以读取自定义配置值。我从 IIS 架构文件夹中删除了客户架构文件,并将applicationHost.configconfigSections部分恢复为出厂设置。

我发现有趣的一件事是 IIS 管理器的配置编辑器公开system.web设置,虽然有一个用于此 ( ASPNET_schema.xml) 的架构applicationHost.config文件,但文件中实际上并未引用架构部分。这让我相信这些文件或sectionSchema名称是以特殊方式处理的。

为此,我解锁了ASPNET_schema.xml文件并添加了我的架构定义:

<sectionSchema name="SimpleConfigGroup">
  <element name="SimpleConfigSection">
    <attribute name="MySetting" 
                     type="string" 
                     validationType="nonEmptyString" />
  </element>
</sectionSchema>
Run Code Online (Sandbox Code Playgroud)

这不起作用,并且SimpleConfigGroup在 IIS 管理器的配置编辑器中不可见。但是没有抛出任何错误,ASP.NET 应用程序仍然可以读取它的自定义配置值。

然后我尝试遵循ASPNET_schema.xml模式文件中使用的约定并添加了这个:

<sectionSchema name="SimpleConfigGroup/SimpleConfigSection">
  <attribute name="MySetting" 
             type="string" 
             validationType="nonEmptyString" />
</sectionSchema>
Run Code Online (Sandbox Code Playgroud)

这实际上有效!:

在此处输入图片说明

ASP.NET 应用程序继续运行,可以读取自定义配置部分,我可以MySetting在 IIS 管理器的配置编辑器中编辑自定义配置部分的值。

然后我回滚ASPNET_schema.xml到出厂设置并尝试SimpleConfigSchema.xml使用相同的定义在 IIS 的架构文件夹中重新创建自定义文件,这也有效。

这也没有添加对 .config 部分的引用applicationHost.config

我得出的结论是,关于扩展 IIS 管理器的配置编辑器和 ASP.NET 需要使用同一部分的架构的指南有点虚假。

Sco*_*MVP 3

嘿凯夫。使用您的示例,这立即对我有用。这是我放置所有东西的地方:

小路:

%systemroot%\system32\inetsrv\config\schema\simpleconfig.xml
Run Code Online (Sandbox Code Playgroud)

内容:

<configSchema>
  <sectionSchema name="SimpleConfigGroup">
    <element name="SimpleConfigSection">
      <attribute name="MySetting" 
                 type="string" 
                 validationType="nonEmptyString" />
    </element>
  </sectionSchema>
</configSchema>
Run Code Online (Sandbox Code Playgroud)

小路:

%systemroot%\system32\inetsrv\config\applicationHost.config (in <configSections> element).
Run Code Online (Sandbox Code Playgroud)

内容:

<section name="SimpleConfigGroup" 
         overrideModeDefault="Allow" 
         allowDefinition="Everywhere" />
Run Code Online (Sandbox Code Playgroud)

小路:

Site's web.config
Run Code Online (Sandbox Code Playgroud)

内容:

  <SimpleConfigGroup>
    <SimpleConfigSection MySetting="Hello World" />
  </SimpleConfigGroup>
Run Code Online (Sandbox Code Playgroud)