• 艾伟_转载: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>


    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>


          通过上面的web.config文件,我本文所使用的自定义节的使用,设置完web.config文件后,我们怎么来使用(获得)我们配置的信息呢? 我们可以从上面看到 type="CWS.Framework.Client.ClientAddressSection,CWS.Framework.Client", 这就告诉我们,通过CWS.Framework.Client命名空间下的ClientAddressSection类来实现这个节的操作。
          为了实现对这个节的使用,需要写一个类,他继承于ConfigurationElement类,实现对config中自定义节CleintAddressCollection中属性的映射,请看下面代码。

    映射web.config自定义类


          通过上面的代码我们可以看到,他实现了对config中CleintAddressCollection属性的映射。在本文中我们只有三个属性,如果有更多的属性我们可以以此类推,实现代码与Config文件中属性的映射关系。
          
          上面我们只是写了一个类来实现对config中属性的映射,但是如何使用这个类呢,请看下面的代码。

    创建自定义节对象

           上面我们在重载方法GetElementKey中指定用Name用为唯一属性。比如说在自定义的节中有多个item,那么我们怎么知道我们需要那一个item呢,这时就需要使用Name来进行标识。

    <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文件,他的作用就是调用上面实现的功能,并把结果输出出来。

    Code


          输出结果如下:


    五.总结
    1.通过此实现为以后开发其它产品实现自定义类提供了参考。
    2.实现功能,需要理解web.config与实际代码之间的映射关系。
    3.对System.Configuration命名空间下的ConfigurationElement,ConfigurationElementCollection,ConfigurationSection类的要有所了解。

  • 相关阅读:
    发张照片纪念下
    以用户为中心的SNS站点数据库设计及实现
    IT点评:腾讯帝国没落的开始从崇敬到平淡,改变从自己开始
    并发下常见的加锁及锁的PHP具体实现
    Ubuntu,Your Linux
    Python初尝试
    C++ Primer Plus读书笔记02
    C++ Primer Plus读书笔记03
    Effective C++读书笔记02
    由扔骰子看平均概率生成
  • 原文地址:https://www.cnblogs.com/waw/p/2157150.html
Copyright © 2020-2023  润新知