深入理解.NET配置文件
在.Net中,配置文件实际分为了两部分,一部分是配置的实际内容,比如appSettings以及上例中的blog和forum结点;另一部分指定结点的处理程序,这些结点位于 configSections 结点下面。
也就是说configSections下面是定义了对配置文件中。处了configSections节点意外的节点的处理程序。之所以这样是有原因的,我们都知道在配置文件下面appSettings节点是这样的形式:
<add key="IV" value="kJlv8Uk0BEngaZL4fQGN6A=="/>
而对于system.web节点却比这个要复杂的多。之所以造成这样的差异是由于不同节点所起的配置功能的不同而造成的,因此对于节点结构的不同,必须要有不同的处理程序来读取这些配置节点中的属性或者值。而定义这些节点的处理程序就是在configSections节点下面进行定义的。
也许你会惊异,默认情况下Web.config文件下是没有configSections节点的。这是因为对于配置文件下的节点.NET都已经有了其读取节点的处理程序。而配置这些节点处理程序就在machine.config文件下面。例如:
appSettings节点
<section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>
connectionStrings节点
<section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false"/>
system.web节点,由于该节点较为复杂,其下面有很多子节点。因此用sectionGroup,为了简练这里中间省略掉很多相同的代码。
<sectionGroup name="system.web" type="System.Web.Configuration.SystemWebSectionGroup, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<section name="anonymousIdentification" type="System.Web.Configuration.AnonymousIdentificationSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition="MachineToApplication"/>
<section name="authentication" type="System.Web.Configuration.AuthenticationSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition="MachineToApplication"/>
<section name="authorization" type="System.Web.Configuration.AuthorizationSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<sectionGroup name="caching" type="System.Web.Configuration.SystemWebCachingSectionGroup, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<section name="cache" type="System.Web.Configuration.CacheSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition="MachineToApplication"/>
<section name="outputCache" type="System.Web.Configuration.OutputCacheSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition="MachineToApplication"/>
<section name="outputCacheSettings" type="System.Web.Configuration.OutputCacheSettingsSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition="MachineToApplication"/>
<section name="sqlCacheDependency" type="System.Web.Configuration.SqlCacheDependencySection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
.NET框架就是通过配置文件的灵活配置,以及这些配置文件的处理程序,和设计模式一起组合使其强大灵活可扩展。
对于.NET配置文件有三个重要的类。ConfigurationManager,Configuration,ConfigurationSection.
ConfigurationManager:
提供对客户端应用程序配置文件的访问。
AppSettings属性可以访问当前应用程序配置的appSetting。
ConnectionStrings 访问配置的ConnectionStringsSection 数据。
public static object GetSection(string sectionName) 检索当前应用程序默认配置的指定配置节,如果配置节指定的dll不在当前项目内会出现错误。
Configuration:表示应用程序的配置文件,也就是对应用程序配置的封装。此类没有构造函数,但是可以通过ConfigurationManger的open方法来初始化此类的实列
ConfigurationSection:表示configSections配置节的实列。
疑问:可以通过Configuration的Sections和SectionGroups属性来获取当前配置文件所定义的configSections或者sectionGroup。但是有一点觉得奇怪,为什么自定义的遍历不到?