ASP.NET Process Model之二:ASP.NET Http Runtime Pipeline[下篇]

  ASP.NET Process Model索引

  • ASP.NET Process Model之一:IIS 和 ASP.NET ISAPI
  • ASP.NET Process Model之二:ASP.NET Http Runtime Pipeline[上篇]
  • ASP.NET Process Model之二:ASP.NET Http Runtime Pipeline[下篇]

  二、ASP.NET Runtime Pipeline

  现在我们真正进入ASP.NET管辖的范畴,下图基本上囊括整个处理过程涉及的对象,接下来我们一起来讨论这一系列的对象如何相互协作去处理Http Request,并最终生成我们所需的Http Response。

  HttpContext

  上面我们介绍了ISAPI在调用ISAPIRuntime的时候将对应的ISAPI ECB Pointer作为参数传递给了ProcessRequest方法,这个ECB pointer可以看成是托管环境和非托管环境进行数据交换的唯一通道,Server Variable和Request Parameter通过它传入ASP.NET作为进一步处理的依据,ASP.NET最后生成的Response通过它传递给ISAPI,并进一步传递给IIS最终返回到Client端。

  借助这个传进来的ECB Pointer,我们创建了一个ISAPIWorkerRequest。ISAPIWorkerRequest作为参数传入HttpRuntime.ProcessRequestNoDemand的调用。HttpRuntime.ProcessRequestNoDemand最终体现在调用ProcessRequestInternal。下面是真个方法的实现:

ProcessRequestInternal
private void ProcessRequestInternal(HttpWorkerRequest wr)
{
HttpContext context;
try
{
context
= new HttpContext(wr, false);
}
catch
{
wr.SendStatus(
400, "Bad Request");
wr.SendKnownResponseHeader(
12, "text/html; charset=utf-8");
byte[] bytes = Encoding.ASCII.GetBytes("<html><body>Bad Request</body></html>");
wr.SendResponseFromMemory(bytes, bytes.Length);
wr.FlushResponse(
true);
wr.EndOfRequest();
return;
}
wr.SetEndOfSendNotification(
this._asyncEndOfSendCallback, context);
Interlocked.Increment(
ref this._activeRequestCount);
HostingEnvironment.IncrementBusyCount();
try
{
try
{
this.EnsureFirstRequestInit(context);
}
catch
{
if (!context.Request.IsDebuggingRequest)
{
throw;
}
}
context.Response.InitResponseWriter();
IHttpHandler applicationInstance
= HttpApplicationFactory.GetApplicationInstance(context);
if (applicationInstance == null)
{
throw new HttpException(SR.GetString("Unable_create_app_object"));
}
if (EtwTrace.IsTraceEnabled(5, 1))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, context.WorkerRequest, applicationInstance.GetType().FullName,
"Start");
}
if (applicationInstance is IHttpAsyncHandler)
{
IHttpAsyncHandler handler2
= (IHttpAsyncHandler) applicationInstance;
context.AsyncAppHandler
= handler2;
handler2.BeginProcessRequest(context,
this._handlerCompletionCallback, context);
}
else
{
applicationInstance.ProcessRequest(context);
this.FinishRequest(context.WorkerRequest, context, null);
}
}
catch (Exception exception)
{
context.Response.InitResponseWriter();
this.FinishRequest(wr, context, exception);
}
}

NET技术ASP.NET Process Model之二:ASP.NET Http Runtime Pipeline[下篇],转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。