DNN學習 之一

來源:互聯網
上載者:User
DNN學習筆記 之一 配置

創 建 人:pblee
建立實踐:2006-6-28
文檔名稱:DNN學習筆記1
關 鍵 字:DNN 源碼分析 配置

DNN是一個開放的平台,不僅僅是原始碼,它支援所有使用者自訂群組件並掛接到主引擎上。正因為它的開放特性,使得它的使用者與日俱增。

本學習筆記基於DNN 4.3源碼,該項目使用.net架構2.0,這樣在學習代碼的同時也可以學習.net 2.0的新特性。

先從設定檔開始
代碼片斷1 release.config
 
  < configSections >
     < sectionGroup  name ="dotnetnuke" >
       <!--  the requirePermission attribute will cause a syntax warning - please ignore - it is required for Medium Trust support -->
       < section  name ="data"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke" />
       < section  name ="logging"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke" />
       < section  name ="scheduling"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke" />
       < section  name ="htmlEditor"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke" />
       < section  name ="navigationControl"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke" />
       < section  name ="searchIndex"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke" />
       < section  name ="searchDataStore"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke" />
       < section  name ="friendlyUrl"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke" />
       < section  name ="caching"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke" />
       < section  name ="authentication"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke"   />
       < section  name ="members"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke"   />
       < section  name ="roles"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke"   />
       < section  name ="profiles"  requirePermission ="false"  type ="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke"   />
     </ sectionGroup >
   </ configSections >


對於configSessions元素MSDN2003的解釋並不深入
[引用:ms-help://MS.MSDNQTR.2003FEB.2052/cpgenref/html/gngrfconfigsectionselementcontainertag.htm]
備忘
如果此元素在設定檔中,則它一定是 <configuration> 元素的第一個子項目。
[引用結束]

MSDN2005的解釋:
[引用:ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/dv_ASPNETgenref/html/8a5cbc84-0257-4c2e-80a9-a064fe7c896b.htm]
指定配置節和命名空間聲明。

   <configSections>
      <section />
      <sectionGroup />
      <remove />
      <clear/>
   </configSections>
子項目
元素  說明 
clear
 移除對繼承的節和節組的所有引用,只允許由當前 section 和 sectionGroup 元素添加的節和節組。
 
remove
 移除對繼承的節和節組的引用。
 
section
 定義配置節處理常式與配置元素之間的關聯。
 
sectionGroup
 定義配置節處理常式與配置節之間的關聯。
 
備忘
configSections 元素指定了配置節和處理常式聲明。由於 ASP.NET 不對如何處理設定檔內的設定作任何假設,因此這非常必要。但 ASP.NET 會將配置資料的處理委託給配置節處理常式。

每個 section 元素標識一個配置節或元素以及對該配置節或元素進行處理的關聯 ConfigurationSection 衍生類別。可以在 sectionGroup 元素中對 section 元素進行邏輯分組,以對 section 元素進行組織並避免命名衝突。section 和 sectionGroup 元素包含在 configSections 元素中。

如果設定檔中包含 configSections 元素,則 configSections 元素必須是 configuration 元素的第一個子項目。
[引用結束]
開啟Machine.Config檔案可以看到我們常用的一些節的定義,比如:
代碼片斷2 Machine.config    < configSections >
     < section  name ="appSettings"  type ="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"  restartOnExternalChanges ="false"  requirePermission ="false"   />
     < section  name ="connectionStrings"  type ="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"  requirePermission ="false"   />
     < 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"   />
……省略一段……
     </ sectionGroup >

也就是說其實config設定檔最重要的地方就是configSections,沒有它我們配置的東西程式不知道如何去處理。做個實驗。
建立一個web項目,在預設產生的web.config的<configuration>元素中加入如下代碼。
範例程式碼1.1    < appSettings  >
     < add  key ="ServerName"  value ="WWW.GOOGLE.COM" />
   </ appSettings >

再在頁面Page_Load()中加入如下代碼:
範例程式碼1.2
     protected   void  Page_Load( object  sender, EventArgs e)
    {
  Response.Write(System.Configuration.ConfigurationSettings.AppSettings[ " ServerName " ]);
    }

瀏覽頁面,很簡單,我們看到了配置中寫下的內容,"WWW.GOOGLE.COM",這個相信很多人都已經會用了
下面我們做點小破壞,刪除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"   />


注意#刪除前確保已經備份源檔案,我們還需要改回來的。
儲存machine.config,再重新整理剛才的頁面,出現錯誤了:

配置錯誤
說明: 在處理向該請求提供服務所需的設定檔時出錯。請檢查下面的特定錯誤詳細資料並適當地修改設定檔。

分析器錯誤資訊: 無法識別的配置節 appSettings。

這個節沒有在web.config以及它繼承的machine.config找到定義。
再改改,把上面刪除的部分添加到web.config相應位置,再重新整理,顯示又正常了。小心,因為你刪除了預設配置,這樣其他本地啟動並執行程式因為還在使用預設配置而沒有在web.config中獨立添加<appsettings>的定義,所有使用它的程式將無法正常使用。把原來的machine.config還原回去吧。

現在我們知道了,所有在web.config中定義的除<configSections>節以外的元素都要從config檔案中定義,包括web.config和它所繼承的machine.config。

OK,我們已經找到起始點了。下面停頓幾秒鐘,試著解釋一下下面的定義,然後繼續往下看。
      <section name="data" requirePermission="false" type="DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke"/>

是的,它聲明了一個節,並告訴CLR如果碰到一個名叫<data>的節將調用"DotNetNuke.Framework.Providers.ProviderConfigurationHandler, DotNetNuke"進行處理。.net FrameWork 為我們提供了一些簡單的配置節處理類庫,參見System.Configuration命名空間。
<section>的RequirePermission屬性可以參見MSDN
http://msdn2.microsoft.com/zh-CN/library/system.configuration.sectioninformation.requirepermission.aspx

當然,下面那些定義基本相同,現在我們只關注<data>,先看看它裡面有什麼屬性,這樣在我們找到處理常式的時候就可以知道它將處理哪些參數了。
代碼片斷3 release.config
     < data 
       defaultProvider ="SqlDataProvider" >
       < providers >
         < clear />
         < add 
           name ="SqlDataProvider"  
          type ="DotNetNuke.Data.SqlDataProvider, DotNetNuke.SqlDataProvider"  
          connectionStringName ="SiteSqlServer"  
          upgradeConnectionString =""  
          providerPath ="~/Providers/DataProviders/SqlDataProvider/"  
          objectQualifier =""
          databaseOwner ="dbo" />
       </ providers >
     </ data >

再看看DotNetNuke.Framework.Providers.ProviderConfigurationHandler如何處理這些配置
代碼片斷4  DotNetNuke.Library/Components/Providers/ProviderConfigurationHandler.vb      Friend   Class  ProviderConfigurationHandler
         Implements  IConfigurationSectionHandler

         Public   Overridable   Overloads   Function  Create( ByVal  parent  As   Object ,  ByVal  context  As   Object ,  ByVal  node  As  System.Xml.XmlNode)  As   Object   Implements  IConfigurationSectionHandler.Create
             Dim  objProviderConfiguration  As   New  ProviderConfiguration
            objProviderConfiguration.LoadValuesFromConfigurationXml(node)
             Return  objProviderConfiguration
         End Function
     End Class

類實現了IConfigurationSectionHandler介面,此介面只有一個方法
Object Create (
 Object parent,
 Object configContext,
 XmlNode section
)
此方法的說明參見MSDN 2005
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref4/html/M_System_Configuration_IConfigurationSectionHandler_Create_2_23718a01.htm

[引用MSDN2005]
您可以用自己的 XML 配置元素來擴充標準的 ASP.NET 配置設定集。若要完成該操作,您必須建立自己的配置節處理常式。

該處理常式必須是一個實現 System.Configuration.IConfigurationSectionHandler 介面或 System.Configuration.ConfigurationSection 類的 .NET Framework 類。
[引用結束]

好,繼續看ProviderConfigurationHandler的實現
            Dim objProviderConfiguration As New ProviderConfiguration
            objProviderConfiguration.LoadValuesFromConfigurationXml(node)
            Return objProviderConfiguration

ProviderConfiguration裡的代碼就不貼出了,他內部包含了一個HashTable,並將配置資訊儲存在裡面。

這樣在程式初始化時就會自動擷取配置資訊了。那麼這個配置如何得到呢。通過System.Configuration.ConfigurationManager類的GetSection方法。需要注意的是此類在.net 1.1中不存在,1.1中使用System.Configuration.ConfigurationSettings.GetSection()得到同樣的效果,但是此方法在2.0中標誌為已到期,也就是.net不保證其向後相容。

詳細資料還請參閱MSDN2005
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref4/html/T_System_Configuration_ConfigurationManager.htm


好了,在我們繼續學習DNN的配置處理之前演練一下我們學到的東

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.