濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > 從請求管道深入剖析HttpModule的實(shí)現(xiàn)機(jī)制圖文介紹

從請求管道深入剖析HttpModule的實(shí)現(xiàn)機(jī)制圖文介紹

熱門標(biāo)簽:保山電話外呼管理系統(tǒng)怎么用 太原外呼電銷機(jī)器人費(fèi)用 淘寶地圖標(biāo)注如何做 朝陽市地圖標(biāo)注 使用智能電話機(jī)器人違法嗎 外呼系統(tǒng)用員工身份證 電話機(jī)器人廣告話術(shù) 蘇州銷售外呼系統(tǒng)預(yù)算 東莞語音電銷機(jī)器人排名
想要了解底層的原理必須對請求處理過程和頁面的生命周期有點(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)原理了。
下面是效果圖

您可能感興趣的文章:
  • Asp.net使用HttpModule壓縮并刪除空白Html請求的實(shí)現(xiàn)代碼
  • asp.net 通過httpModule計(jì)算頁面的執(zhí)行時(shí)間
  • asp.net通過HttpModule自動(dòng)在Url地址上添加參數(shù)
  • HttpHandler HttpModule入門篇

標(biāo)簽:西藏 克拉瑪依 運(yùn)城 潛江 呼倫貝爾 阿里 綏化 洛陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《從請求管道深入剖析HttpModule的實(shí)現(xiàn)機(jī)制圖文介紹》,本文關(guān)鍵詞  從,請求,管道,深入,剖析,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《從請求管道深入剖析HttpModule的實(shí)現(xiàn)機(jī)制圖文介紹》相關(guān)的同類信息!
  • 本頁收集關(guān)于從請求管道深入剖析HttpModule的實(shí)現(xiàn)機(jī)制圖文介紹的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    财经| 津市市| 阿尔山市| 武宁县| 贵港市| 淮安市| 柏乡县| 乌苏市| 永善县| 进贤县| 望江县| 尚志市| 海丰县| 白银市| 临武县| 囊谦县| 寻乌县| 海口市| 城口县| 青岛市| 临夏市| 汉沽区| 即墨市| 周宁县| 彰化市| 达孜县| 息烽县| 贵港市| 时尚| 龙门县| 苏尼特右旗| 平阳县| 丰台区| 聂荣县| 无棣县| 徐汇区| 湖北省| 新巴尔虎右旗| 搜索| 诏安县| 滨海县|