ASP.NET中寫自定義的Config Provider

作者: 我-大熊  來源: 博客園  發布時間: 2009-08-25 13:09  閱讀: 2545 次  推薦: 0   原文鏈接   [收藏]  

一.寫作前題

    我們用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類的要有所了解。

0
0
 
標簽:ASP.NET
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()