欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

从请求管道深入剖析HttpModule的实现机制图文介绍

程序员文章站 2024-03-05 13:12:12
想要了解底层的原理必须对请求处理过程和页面的生命周期有点了解才方便您入门学习一下内容: 关于请求处理过程和页面的生命周期将会在接下来的日子为大家做一个深入的讲解。 http...
想要了解底层的原理必须对请求处理过程和页面的生命周期有点了解才方便您入门学习一下内容:
关于请求处理过程和页面的生命周期将会在接下来的日子为大家做一个深入的讲解。
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
复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace httpapplicationprocessmethoddemo
{
/// <summary>
/// 上下文
/// </summary>
public class httpcontext
{
}
}

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的实现机制图文介绍