[原创] ASP.NET中写自定义的Config Provider(提供源代码下载)
本文只代表作者在一定阶段的认识与理解。
一.写作前题
我们用ASP.NET做项目开发的时候,配置Config文件那是经常的事情,VS.NET的Config文件提供了很多节,但是往往提供的这些配置信息还不能够完全满足我们的项目开发需求,而且微软正是考虑到这方面的因素,他允许用户自定义Configuration的相关配置内容。本文就此写了一些实例,希望对大家有所帮助。
二.本文内容
1.实现web.config中的自定义
2.对自定义节的使用
3.本文总结
三.实现Web.Config中自定义节
废话不多说,直接说主题,这里我们要继承ConfigurationElement,ConfigurationElementCollection,ConfigurationSection等相关的类。
首先我们在Config文件中的增加了一个节,我们增加了一个自定义节<section name="commonSectionConfiguration" type="CWS.Framework.Client.ClientAddressSection,CWS.Framework.Client"/>,这个节的具体配置如下所示
1 <commonSectionConfiguration>
2 <CleintAddressCollection>
3 <add Name="CommonPath" ServiceCommonPath="http://localhost/CWSHost/SVCService/{0}" IsDefault="true"></add>
4 </CleintAddressCollection>
5 </commonSectionConfiguration>
2 <CleintAddressCollection>
3 <add Name="CommonPath" ServiceCommonPath="http://localhost/CWSHost/SVCService/{0}" IsDefault="true"></add>
4 </CleintAddressCollection>
5 </commonSectionConfiguration>
Config文件的整体配置如下所示:
1 <configuration>
2 <configSections>
3 <section name="commonSectionConfiguration" type="CWS.Framework.Client.ClientAddressSection,CWS.Framework.Client"/>
4 <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
5 <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
6 <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
7 <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
8 <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
9 <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
10 <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
11 <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
12 </sectionGroup>
13 </sectionGroup>
14 </sectionGroup>
15 </configSections>
16 <commonSectionConfiguration>
17 <CleintAddressCollection>
18 <add Name="CommonPath" ServiceCommonPath="http://localhost/CWSHost/SVCService/{0}" IsDefault="true"></add>
19 </CleintAddressCollection>
20 </commonSectionConfiguration>
21 </configuration>
2 <configSections>
3 <section name="commonSectionConfiguration" type="CWS.Framework.Client.ClientAddressSection,CWS.Framework.Client"/>
4 <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
5 <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
6 <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
7 <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
8 <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
9 <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
10 <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
11 <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
12 </sectionGroup>
13 </sectionGroup>
14 </sectionGroup>
15 </configSections>
16 <commonSectionConfiguration>
17 <CleintAddressCollection>
18 <add Name="CommonPath" ServiceCommonPath="http://localhost/CWSHost/SVCService/{0}" IsDefault="true"></add>
19 </CleintAddressCollection>
20 </commonSectionConfiguration>
21 </configuration>
通过上面的web.config文件,我本文所使用的自定义节的使用,设置完web.config文件后,我们怎么来使用(获得)我们配置的信息呢? 我们可以从上面看到 type="CWS.Framework.Client.ClientAddressSection,CWS.Framework.Client", 这就告诉我们,通过CWS.Framework.Client命名空间下的ClientAddressSection类来实现这个节的操作。
为了实现对这个节的使用,需要写一个类,他继承于ConfigurationElement类,实现对config中自定义节CleintAddressCollection中属性的映射,请看下面代码。
通过上面的代码我们可以看到,他实现了对config中CleintAddressCollection属性的映射。在本文中我们只有三个属性,如果有更多的属性我们可以以此类推,实现代码与Config文件中属性的映射关系。
上面我们只是写了一个类来实现对config中属性的映射,但是如何使用这个类呢,请看下面的代码。
<commonSectionConfiguration>
<CleintAddressCollection>
<add Name="CommonPath" ServiceCommonPath="http://localhost/CWSHost/SVCService/{0}" IsDefault="true"></add>
<add Name="CommonPath1" ServiceCommonPath="http://localhost/DUPHost/SVCService/{0}" IsDefault="true"></add>
</CleintAddressCollection>
</commonSectionConfiguration>
<CleintAddressCollection>
<add Name="CommonPath" ServiceCommonPath="http://localhost/CWSHost/SVCService/{0}" IsDefault="true"></add>
<add Name="CommonPath1" ServiceCommonPath="http://localhost/DUPHost/SVCService/{0}" IsDefault="true"></add>
</CleintAddressCollection>
</commonSectionConfiguration>
下面这段代码的作用,是取出我们所需要的那个section的内容,因为我们可能有多个section的类容,我们怎么知道我们现在需要使用那个section的内容,比如说下面的代码,我们就是告诉我们需要取出CleintAddressCollection节中的信息。
四.引用自定义节
上面第三节内容的代码我们已经实现了地自定节的实现和操作,下面的类是一个aspx.cs文件,他的作用就是调用上面实现的功能,并把结果输出出来。
输出结果如下:
五.总结
1.通过此实现为以后开发其它产品实现自定义类提供了参考。
2.实现功能,需要理解web.config与实际代码之间的映射关系。
3.对System.Configuration命名空间下的ConfigurationElement,ConfigurationElementCollection,ConfigurationSection类的要有所了解。
源代码下载