ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-10 19:03:58  阅读:443  来源: 互联网

标签:HttpRuntime mvchandler 源码 wr context ._ httpContext mvc internal


上一节说在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);
        }

 

标签:HttpRuntime,mvchandler,源码,wr,context,._,httpContext,mvc,internal
来源: https://www.cnblogs.com/NNYang/p/10999558.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有