mvc5 源码解析2-1:mvchandler的执行
2024-10-20 04:03:22
上一节说在urlroutingmodule中mvchandler 映射到httpcontext上,那mvchandler又是怎么执行的呢?
(1)、httpruntime
从isapiruntime pr方法到httpruntime
ProcessRequestNoDemand(wr)方法
// System.Web.HttpRuntime
internal static void ProcessRequestNoDemand(HttpWorkerRequest wr)
{
//获取请求队列
RequestQueue requestQueue = HttpRuntime._theRuntime._requestQueue;
wr.UpdateInitialCounters();
if (requestQueue != null)
{
wr = requestQueue.GetRequestToExecute(wr);
}
if (wr != null)
{
HttpRuntime.CalculateWaitTimeAndUpdatePerfCounter(wr);
wr.ResetStartTime();
HttpRuntime.ProcessRequestNow(wr);
}
}
ProcessRequestNow(wr)方法
// System.Web.HttpRuntime
internal static void ProcessRequestNow(HttpWorkerRequest wr)
{
HttpRuntime._theRuntime.ProcessRequestInternal(wr);
}
ProcessRequestInternal(wr)方法
// System.Web.HttpRuntime
private void ProcessRequestInternal(HttpWorkerRequest wr)
{
//活动请求数量增加
Interlocked.Increment(ref this._activeRequestCount);
//判断请求数异常
if (this._disposingHttpRuntime)
{
try
{
wr.SendStatus(, "Server Too Busy");
wr.SendKnownResponseHeader(, "text/html; charset=utf-8");
byte[] bytes = Encoding.ASCII.GetBytes("<html><body>Server Too Busy</body></html>");
wr.SendResponseFromMemory(bytes, bytes.Length);
wr.FlushResponse(true);
wr.EndOfRequest();
}
finally
{
Interlocked.Decrement(ref this._activeRequestCount);
}
return;
}
HttpContext httpContext;
try
{
//HttpWorkerRequest转HttpContext
httpContext = new HttpContext(wr, false);
}
catch
{
try
{
wr.SendStatus(, "Bad Request");
wr.SendKnownResponseHeader(, "text/html; charset=utf-8");
byte[] bytes2 = Encoding.ASCII.GetBytes("<html><body>Bad Request</body></html>");
wr.SendResponseFromMemory(bytes2, bytes2.Length);
wr.FlushResponse(true);
wr.EndOfRequest();
return;
}
finally
{
Interlocked.Decrement(ref this._activeRequestCount);
}
}
wr.SetEndOfSendNotification(this._asyncEndOfSendCallback, httpContext);
HostingEnvironment.IncrementBusyCount();
try
{
try
{
this.EnsureFirstRequestInit(httpContext);
}
catch
{
if (!httpContext.Request.IsDebuggingRequest)
{
throw;
}
}
httpContext.Response.InitResponseWriter();
//获取application实例
IHttpHandler applicationInstance = HttpApplicationFactory.GetApplicationInstance(httpContext);
if (applicationInstance == null)
{
throw new HttpException(SR.GetString("Unable_create_app_object"));
}
if (EtwTrace.IsTraceEnabled(, ))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, httpContext.WorkerRequest, applicationInstance.GetType().FullName, "Start");
}
//判断application实例是否有继承IHttpAsyncHandler
if (applicationInstance is IHttpAsyncHandler)
{
IHttpAsyncHandler httpAsyncHandler = (IHttpAsyncHandler)applicationInstance;
httpContext.AsyncAppHandler = httpAsyncHandler;
//执行application的BeginProcessRequest
httpAsyncHandler.BeginProcessRequest(httpContext, this._handlerCompletionCallback, httpContext);
}
else
{
applicationInstance.ProcessRequest(httpContext);
this.FinishRequest(httpContext.WorkerRequest, httpContext, null);
}
}
catch (Exception e)
{
httpContext.Response.InitResponseWriter();
this.FinishRequest(wr, httpContext, e);
}
}
BeginProcessRequest方法
IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
this._context = context;
this._context.ApplicationInstance = this;
this._stepManager.InitRequest();
this._context.Root();
HttpAsyncResult httpAsyncResult = new HttpAsyncResult(cb, extraData);
this.AsyncResult = httpAsyncResult;
if (this._context.TraceIsEnabled)
{
HttpRuntime.Profile.StartRequest(this._context);
}
//执行步骤
this.ResumeSteps(null);
return httpAsyncResult;
}
ResumeSteps方法
private void ResumeSteps(Exception error)
{
this._stepManager.ResumeSteps(error);
}
this._stepManager 抽象类
internal abstract class StepManager
{
protected HttpApplication _application; protected bool _requestCompleted; internal bool IsCompleted
{
get
{
return this._requestCompleted;
}
} internal StepManager(HttpApplication application)
{
this._application = application;
} internal abstract void BuildSteps(WaitCallback stepCallback); internal void CompleteRequest()
{
this._requestCompleted = true;
if (HttpRuntime.UseIntegratedPipeline)
{
HttpContext context = this._application.Context;
if (context != null && context.NotificationContext != null)
{
context.NotificationContext.RequestCompleted = true;
}
}
} internal abstract void InitRequest(); internal abstract void ResumeSteps(Exception error);
}
最新文章
- saltstack(主机改名)
- Java语言程序设计(基础篇)第二章
- 5.2视图中的Order by
- Win7系统修改hosts文件不能保存的解决方法
- osx 文本编辑工具下载地址Sublime Text 3
- Yii源码阅读笔记(十六)
- Mongo:将查询结果转换为自定义类
- Google 网站打不开
- cf D. Renting Bikes
- DNS服务器 和CDN
- React Fiber源码分析 第三篇(异步状态)
- HBase基础架构及原理
- JavaScript与正则表达式
- tf.identity 个人理解
- IDEA 调整 VM 配置文件(64位)
- 全网最详细的用pip安装****模块报错:Could not find a version that satisfies the requirement ****(from version:) No matching distribution found for ****的解决办法(图文详解)
- Linux 修改root密码(忘记密码后)
- String的一些方法试探
- java maven 安装
- 【OpenCV学习笔记】三十、轮廓特征属性及应用(七)—位置关系及轮廓匹配