標籤:自訂 nbsp sum main blog dem 介面 註冊 方法
想要瞭解底層的原理必須對請求處理過程和頁面的生命週期有點瞭解才方便您入門學習一下內容:
關於請求處理過程和頁面的生命週期將會在接下來的日子為大家做一個深入的講解。
HttpModule的實現機制如下:
1.請求到達ISAPIRuntime 的時候通過ProcessReqeust(下文統稱pr ) 方法建立 HttpWrokRequest 對象。
2.在執行ISAPIRuntime 的pr 方法時候,方法內部的HttpRuntime 的pr 方法根據HttpWorkRequest 對象建立了內容物件 HttpContext 。
3.在HttpRuntime 的 pr 方法內部又通過 HttpApplicationFactory 建立了一個處理應用程式的 HttpApplication 執行個體。
注意:HttpApplication的建立是根據Global.asax檔案編譯後的類型,再通過反射的方法建立的執行個體,由於建立執行個體的過程非常消耗時間和資源,這個則使用了對象池技術
4.在建立HttpApplication 執行個體的過程中,內部會調用InitInternal 方法,在這個方法裡面 調用了HttpModule 的初始化方法,實現了事件的註冊。
注意:在實現事件的註冊的過程中,內部會去設定檔裡面找是否有配置HttpModule模組,如果有則通過反射註冊,沒有則繼續往下走,直到方法跳出。這個過程就是微軟的外掛程式機制的體現。
5.事件註冊完之後,HttpApplication執行個體則開始調用自己的pr 方法開始執行頁面的生命週期了。
總結:HttpModule 模組的事件註冊,就是在HttpApplication 執行個體內部調用InitInternal 方法,這個方法裡面 調用了HttpModule 的初始化方法,實現了事件的註冊。
下面的代碼是協助你理解這個過程:
1、首先定義一個上下文 類 HttpContext
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace HttpApplicationProcessMethodDemo 7 { 8 /// <summary> 9 /// 上下文10 /// </summary>11 public class HttpContext12 {13 }14 }
2.定義兩個介面分別為: IHttpHandler 、IHttpModule
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace HttpApplicationProcessMethodDemo{ /// <summary> /// 介面,這個介面主要是在application調用pr方法的時候 /// 實現調用具體頁面或一般處理常式的pr方法的。 /// </summary> public interface IHttpHandler { void ProcessRequest(HttpContext context); }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace HttpApplicationProcessMethodDemo{ /// <summary> /// 介面,這個介面主要類比在Application 的 InitInternal方法內部實現事件的註冊 /// </summary> public interface IHttpModule { void Init(HttpApplication application); }}
3、定義一個頁面類 Page
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace HttpApplicationProcessMethodDemo{ /// <summary> /// 頁面類 /// </summary> public class Page:IHttpHandler { /// <summary> /// 實現了IHttpHandler介面 /// </summary> /// <param name="context">上下文</param> public void ProcessRequest(HttpContext context) { Console.WriteLine("頁面的生命週期...."); Console.WriteLine(".................."); Console.WriteLine(".................."); Console.WriteLine(".................."); Console.WriteLine("頁面的生命週期結束..."); } }}
4.定義一個應用程式類 Application
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace HttpApplicationProcessMethodDemo{ public class HttpApplication:IHttpHandler { //初始化方法 public void InitInternal() { //從設定檔中讀取所有的註冊了HttpModule的程式集,然後通過反射出執行個體,並調用Init方法!!! 下面的MyHttpModule假設是通過反射出來的 IHttpModule httpModule = new MyHttpModule(); httpModule.Init(this); BindEvent(); } //Application 自己的事件回應程式法 private void BindEvent() { BeginRequest += new EventHandler(HttpApplication_BeginRequest); PostResolveRequestCache += new EventHandler(HttpApplication_PostResolveRequestCache); EndRequest += new EventHandler(HttpApplication_EndRequest); } void HttpApplication_EndRequest(object sender, EventArgs e) { Console.WriteLine("application自己的事件回應程式法執行了--EndRequest"); } void HttpApplication_PostResolveRequestCache(object sender, EventArgs e) { Console.WriteLine("application自己的事件回應程式法執行了--PostResolveRequest"); } void HttpApplication_BeginRequest(object sender, EventArgs e) { Console.WriteLine("application自己的事件回應程式法執行了--BeginRequest"); } //把此方法看成是 http 請求處理的管道 public void ProcessRequest(HttpContext context) { //19個事件,23個步驟 Console.WriteLine("開始請求"); //觸發第一個事件 BeginRequest(this, null); //觸發第七個事件 PostResolveRequestCache(this, null); Console.WriteLine("已經擷取緩衝"); //第七個和第八個事件之間,建立頁面對象或一般處理常式 IHttpHandler httpHandler = new Page(); Console.WriteLine("建立頁面對象"); //在11 和 12 個事件之間執行pr方法 Console.WriteLine("開始執行頁面的生命週期"); httpHandler.ProcessRequest(context); //最後一個事件 EndRequest(this, null); Console.WriteLine("結束請求"); } public event EventHandler BeginRequest; public event EventHandler PostResolveRequestCache; public event EventHandler EndRequest; }}
5.類比請求管道中的執行過程
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace HttpApplicationProcessMethodDemo{ class Program { static void Main(string[] args) { //ISAPIRuntime //假設根據請求建立了HttpContext上下文 HttpContext context = new HttpContext(); //假設從HttpApplicationFactory建立出來 HttpApplication application = new HttpApplication(); //把所有註冊在設定檔中的HttpModule載入並執行其Init方法 application.InitInternal(); //調用pr方法開始執行頁面的pr方法 application.ProcessRequest(context); Console.ReadKey(); } }}
6.自訂一個HttpModule
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace HttpApplicationProcessMethodDemo{ /// <summary> /// 自訂的HttpModule模組 /// </summary> public class MyHttpModule : IHttpModule { /// <summary> /// 實現了IHttpModule介面 /// </summary> /// <param name="application"></param> public void Init(HttpApplication application) { //註冊事件 application.BeginRequest += new EventHandler(application_BeginRequest); application.PostResolveRequestCache += new EventHandler(application_PostResolveRequestCache); application.EndRequest += new EventHandler(application_EndRequest); } void application_EndRequest(object sender, EventArgs e) { Console.WriteLine("HttpModule註冊了EndRequest方法"); } void application_PostResolveRequestCache(object sender, EventArgs e) { Console.WriteLine("HttpModule註冊了PostResolveRequestCache方法"); } void application_BeginRequest(object sender, EventArgs e) { Console.WriteLine("HttpModule註冊了BeginRequest方法"); } }}
通過以上的步驟就實現了整個管道的執行過程和HttpModule的實現原理了。
下面是:
從請求管道深入剖析HttpModule的實現機制,有圖有真相