想要了解底層的原理必須對請求處理過程和頁面的生命周期有點(diǎn)了解才方便您入門學(xué)習(xí)一下內(nèi)容:
關(guān)于請求處理過程和頁面的生命周期將會(huì)在接下來的日子為大家做一個(gè)深入的講解。
HttpModule的實(shí)現(xiàn)機(jī)制如下:
1.請求到達(dá)ISAPIRuntime 的時(shí)候通過ProcessReqeust(下文統(tǒng)稱pr ) 方法創(chuàng)建 HttpWrokRequest 對象。
2.在執(zhí)行ISAPIRuntime 的pr 方法時(shí)候,方法內(nèi)部的HttpRuntime 的pr 方法根據(jù)HttpWorkRequest 對象創(chuàng)建了上下文對象 HttpContext 。
3.在HttpRuntime 的 pr 方法內(nèi)部又通過 HttpApplicationFactory 創(chuàng)建了一個(gè)處理應(yīng)用程序的 HttpApplication 實(shí)例。
注意:HttpApplication的創(chuàng)建是根據(jù)Global.asax文件編譯后的類型,再通過反射的方法創(chuàng)建的實(shí)例,由于創(chuàng)建實(shí)例的過程非常消耗時(shí)間和資源,這個(gè)則使用了對象池技術(shù)
4.在創(chuàng)建HttpApplication 實(shí)例的過程中,內(nèi)部會(huì)調(diào)用InitInternal 方法,在這個(gè)方法里面 調(diào)用了HttpModule 的初始化方法,實(shí)現(xiàn)了事件的注冊。
注意:在實(shí)現(xiàn)事件的注冊的過程中,內(nèi)部會(huì)去配置文件里面找是否有配置HttpModule模塊,如果有則通過反射注冊,沒有則繼續(xù)往下走,直到方法跳出。這個(gè)過程就是微軟的插件機(jī)制的體現(xiàn)。
5.事件注冊完之后,HttpApplication實(shí)例則開始調(diào)用自己的pr 方法開始執(zhí)行頁面的生命周期了。
總結(jié):HttpModule 模塊的事件注冊,就是在HttpApplication 實(shí)例內(nèi)部調(diào)用InitInternal 方法,這個(gè)方法里面 調(diào)用了HttpModule 的初始化方法,實(shí)現(xiàn)了事件的注冊。
下面的代碼是幫助你理解這個(gè)過程:
1、首先定義一個(gè)上下文 類 HttpContext
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HttpApplicationProcessMethodDemo
{
/// summary>
/// 上下文
/// /summary>
public class HttpContext
{
}
}
2.定義兩個(gè)接口分別為: IHttpHandler 、IHttpModule
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HttpApplicationProcessMethodDemo
{
/// summary>
/// 接口,這個(gè)接口主要是在application調(diào)用pr方法的時(shí)候
/// 實(shí)現(xiàn)調(diào)用具體頁面或一般處理程序的pr方法的。
/// /summary>
public interface IHttpHandler
{
void ProcessRequest(HttpContext context);
}
}
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HttpApplicationProcessMethodDemo
{
/// summary>
/// 接口,這個(gè)接口主要模擬在Application 的 InitInternal方法內(nèi)部實(shí)現(xiàn)事件的注冊
/// /summary>
public interface IHttpModule
{
void Init(HttpApplication application);
}
}
3、定義一個(gè)頁面類 Page
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HttpApplicationProcessMethodDemo
{
/// summary>
/// 頁面類
/// /summary>
public class Page:IHttpHandler
{
/// summary>
/// 實(shí)現(xiàn)了IHttpHandler接口
/// /summary>
/// param name="context">上下文/param>
public void ProcessRequest(HttpContext context)
{
Console.WriteLine("頁面的生命周期....");
Console.WriteLine("..................");
Console.WriteLine("..................");
Console.WriteLine("..................");
Console.WriteLine("頁面的生命周期結(jié)束...");
}
}
}
4.定義一個(gè)應(yīng)用程序類 Application
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HttpApplicationProcessMethodDemo
{
public class HttpApplication:IHttpHandler
{
//初始化方法
public void InitInternal()
{
//從配置文件中讀取所有的注冊了HttpModule的程序集,然后通過反射出實(shí)例,并調(diào)用Init方法?。?! 下面的MyHttpModule假設(shè)是通過反射出來的
IHttpModule httpModule = new MyHttpModule();
httpModule.Init(this);
BindEvent();
}
//Application 自己的事件響應(yīng)方法
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自己的事件響應(yīng)方法執(zhí)行了--EndRequest");
}
void HttpApplication_PostResolveRequestCache(object sender, EventArgs e)
{
Console.WriteLine("application自己的事件響應(yīng)方法執(zhí)行了--PostResolveRequest");
}
void HttpApplication_BeginRequest(object sender, EventArgs e)
{
Console.WriteLine("application自己的事件響應(yīng)方法執(zhí)行了--BeginRequest");
}
//把此方法看成是 http 請求處理的管道
public void ProcessRequest(HttpContext context)
{
//19個(gè)事件,23個(gè)步驟
Console.WriteLine("開始請求");
//觸發(fā)第一個(gè)事件
BeginRequest(this, null);
//觸發(fā)第七個(gè)事件
PostResolveRequestCache(this, null);
Console.WriteLine("已經(jīng)獲取緩存");
//第七個(gè)和第八個(gè)事件之間,創(chuàng)建頁面對象或一般處理程序
IHttpHandler httpHandler = new Page();
Console.WriteLine("創(chuàng)建頁面對象");
//在11 和 12 個(gè)事件之間執(zhí)行pr方法
Console.WriteLine("開始執(zhí)行頁面的生命周期");
httpHandler.ProcessRequest(context);
//最后一個(gè)事件
EndRequest(this, null);
Console.WriteLine("結(jié)束請求");
}
public event EventHandler BeginRequest;
public event EventHandler PostResolveRequestCache;
public event EventHandler EndRequest;
}
}
5.模擬請求管道中的執(zhí)行過程
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HttpApplicationProcessMethodDemo
{
class Program
{
static void Main(string[] args)
{
//ISAPIRuntime
//假設(shè)根據(jù)請求創(chuàng)建了HttpContext上下文
HttpContext context = new HttpContext();
//假設(shè)從HttpApplicationFactory創(chuàng)建出來
HttpApplication application = new HttpApplication();
//把所有注冊在配置文件中的HttpModule加載并執(zhí)行其Init方法
application.InitInternal();
//調(diào)用pr方法開始執(zhí)行頁面的pr方法
application.ProcessRequest(context);
Console.ReadKey();
}
}
}
6.自定義一個(gè)HttpModule
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HttpApplicationProcessMethodDemo
{
/// summary>
/// 自定義的HttpModule模塊
/// /summary>
public class MyHttpModule : IHttpModule
{
/// summary>
/// 實(shí)現(xiàn)了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方法");
}
}
}
通過以上的步驟就實(shí)現(xiàn)了整個(gè)管道的執(zhí)行過程和HttpModule的實(shí)現(xiàn)原理了。
下面是效果圖:
![](/d/20211017/2d1f5b19d83370e61860a8907564e4db.gif)
您可能感興趣的文章:- Asp.net使用HttpModule壓縮并刪除空白Html請求的實(shí)現(xiàn)代碼
- asp.net 通過httpModule計(jì)算頁面的執(zhí)行時(shí)間
- asp.net通過HttpModule自動(dòng)在Url地址上添加參數(shù)
- HttpHandler HttpModule入門篇