mvc源码解析2-1:mvchandler的执行

上一节说在urlroutingmodule中mvchandler 映射到httpcontext上,那mvchandler又是怎么执行的呢?

(1)、httpruntime

         Processrequest方法

  

// System.Web.HttpRuntime
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
public static void ProcessRequest(HttpWorkerRequest wr)
{
    if (wr == null)
    {
        throw new ArgumentNullException("wr");
    }
  //是否集成模式
  if (HttpRuntime.UseIntegratedPipeline) { throw new PlatformNotSupportedException(SR.GetString("Method_Not_Supported_By_Iis_Integrated_Mode", new object[] { "HttpRuntime.ProcessRequest" })); } HttpRuntime.ProcessRequestNoDemand(wr); }

  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(503, "Server Too Busy"); wr.SendKnownResponseHeader(12, "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(400, "Bad Request"); wr.SendKnownResponseHeader(12, "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(5, 1)) { 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);
        }

 

上一篇:Docker Compose 容器编排


下一篇:OLED显示实验源程序