ASP.NET自定义configuration节声明中断了IISpipe理器configuration编辑器

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

在我的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> 

有几个类可以访问驻留在名为CustomSettingsLib类库项目中的此自定义configuration部分:

SimpleConfigGroup.cs:

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

SimpleConfigSection.cs:

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

读取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); } } } 

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

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

我添加了一个IISconfiguration架构扩展文件来:

%systemroot%\system32\inetsrv\config\schema

 <configSchema> <sectionSchema name="SimpleConfigGroup"> <element name="SimpleConfigSection"> <attribute name="MySetting" type="string" validationType="nonEmptyString" /> </element> </sectionSchema> </configSchema> 

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

 <section name="SimpleConfigGroup" overrideModeDefault="Allow" allowDefinition="Everywhere" /> 

我遇到的问题是,当我打开IISpipe理器的configuration编辑器的网站:

在这里输入图像说明

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

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

这里是这个错误的屏幕截图:

在这里输入图像说明

如果我从站点的web.config文件中删除了.NET <sectionGroup>声明,我可以使用IISpipe理器的configuration编辑器来编辑自定义设置。 但是,我的Web应用程序无法运行,因为<sectionGroup>configuration信息是自定义configuration部分所必需的,而.NET则能够parsingweb.config文件。

我已经尝试将<sectionGroup>添加到根machine.config (甚至将configuration程序集放入.NET 2.0框架程序集文件夹),但是我得到相同的错误。 我也尝试签署configuration大会认为可能有一个信任问题,但这也没有帮助。

我觉得奇怪的是, machine.config中的通常.NET Framework <configSections>不会打乱IISpipe理器configurationpipe理器,也不会影响ASP.NET 2.0站点中的.NET 3.5 System.Web.Extensions <sectionGroup>定义,例如system.web但我的自定义configuration部分。

为什么是这样? 它是IISpipe理器configuration编辑器中的错误吗?

更新:

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

 <sectionGroup name="SimpleConfigGroup"> <section name="SimpleConfigSection" allowDefinition="Everywhere" overrideModeDefault="Allow" /> </sectionGroup> 

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

在这里输入图像说明


我尝试从web.config删除sectionGroup/section声明,但保留sectionGroup中的上述sectionGroup声明。 IISpipe理器的configuration编辑器不再列出SimpleConfigGroup部分。


然后我在applicationHost.config试过这个:

 <section name="SimpleConfigGroup" allowDefinition="Everywhere" overrideModeDefault="Allow" type="CustomSettingsLib.SimpleConfigGroup, CustomSettingsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7a58d3af5d6768c9"/> 

IISpipe理器的configuration设置编辑器现在可以再次看到该部分,但没有错误,但由于web.config没有节声明,ASP.NET应用程序会抛出“无法识别的configuration节”exception。


我也在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> 

同样,没有骰子,ASP.NET会抛出“无法识别的configuration节”exception,IISpipe理器的configuration编辑器不再列出SimpleConfigGroup部分。


进展:

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

我发现有趣的事情之一是,IISpipe理器的configuration编辑器公开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> 

这不起作用, SimpleConfigGroup在IISpipe理器的configuration编辑器中不可见。 但是没有发生错误,ASP.NET应用程序仍然可以读取它的自定义configuration值。

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

 <sectionSchema name="SimpleConfigGroup/SimpleConfigSection"> <attribute name="MySetting" type="string" validationType="nonEmptyString" /> </sectionSchema> 

这实际上工作!:

在这里输入图像说明

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

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

这也没有添加对applicationHost.config的configuration节的引用。

我得出的结论是扩展架构的指导,其中IISpipe理器的configuration编辑器和ASP.NET需要使用相同的部分略有不实。

嘿Kev。 这对我来说就马上用你的例子。 这是我放置一切的地方:

path:

 %systemroot%\system32\inetsrv\config\schema\simpleconfig.xml 

内容:

 <configSchema> <sectionSchema name="SimpleConfigGroup"> <element name="SimpleConfigSection"> <attribute name="MySetting" type="string" validationType="nonEmptyString" /> </element> </sectionSchema> </configSchema> 

path:

 %systemroot%\system32\inetsrv\config\applicationHost.config (in <configSections> element). 

内容:

 <section name="SimpleConfigGroup" overrideModeDefault="Allow" allowDefinition="Everywhere" /> 

path:

 Site's web.config 

内容:

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