mvc5 源码解析2-2 mvchandler的执行
我们从application获取的时候查看stepmanager的实现类
IHttpHandler applicationInstance = HttpApplicationFactory.GetApplicationInstance(httpContext);
/ System.Web.HttpApplicationFactory
internal static IHttpHandler GetApplicationInstance(HttpContext context)
{
if (HttpApplicationFactory._customApplication != null)
{
return HttpApplicationFactory._customApplication;
}
if (context.Request.IsDebuggingRequest)
{
return new HttpDebugHandler();
}
HttpApplicationFactory._theApplicationFactory.EnsureInited();
HttpApplicationFactory._theApplicationFactory.EnsureAppStartCalled(context);
return HttpApplicationFactory._theApplicationFactory.GetNormalApplicationInstance(context);
}
/ System.Web.HttpApplicationFactory
private HttpApplication GetNormalApplicationInstance(HttpContext context)
{
HttpApplication httpApplication = null;
if (!this._freeList.TryTake(out httpApplication))
{
//创建application
httpApplication = (HttpApplication)HttpRuntime.CreateNonPublicInstance(this._theApplicationType);
using (new ApplicationImpersonationContext())
{
//初始化application
httpApplication.InitInternal(context, this._state, this._eventHandlerMethods);
}
}
if (AppSettings.UseTaskFriendlySynchronizationContext)
{
httpApplication.ApplicationInstanceConsumersCounter = new CountdownTask();
Task arg_8A_0 = httpApplication.ApplicationInstanceConsumersCounter.Task;
Action<Task, object> arg_8A_1;
if ((arg_8A_1 = HttpApplicationFactory.<>c.<>9__34_0) == null)
{
arg_8A_1 = (HttpApplicationFactory.<>c.<>9__34_0 = new Action<Task, object>(HttpApplicationFactory.<>c.<>.<GetNormalApplicationInstance>b__34_0));
}
arg_8A_0.ContinueWith(arg_8A_1, httpApplication, TaskContinuationOptions.ExecuteSynchronously);
}
return httpApplication;
}
初始化application
// System.Web.HttpApplication
internal void InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers)
{
this._state = state;
PerfCounters.IncrementCounter(AppPerfCounter.PIPELINES);
try
{
try
{
this._initContext = context;
this._initContext.ApplicationInstance = this;
context.ConfigurationPath = context.Request.ApplicationPathObject;
using (new DisposableHttpContextWrapper(context))
{
if (HttpRuntime.UseIntegratedPipeline)
{
try
{
context.HideRequestResponse = true;
this._hideRequestResponse = true;
this.InitIntegratedModules();
goto IL_6B;
}
finally
{
context.HideRequestResponse = false;
this._hideRequestResponse = false;
}
}
this.InitModules();
IL_6B:
if (handlers != null)
{
this.HookupEventHandlersForApplicationAndModules(handlers);
}
this._context = context;
if (HttpRuntime.UseIntegratedPipeline && this._context != null)
{
this._context.HideRequestResponse = true;
}
this._hideRequestResponse = true;
try
{
this.Init();
}
catch (Exception error)
{
this.RecordError(error);
}
}
if (HttpRuntime.UseIntegratedPipeline && this._context != null)
{
this._context.HideRequestResponse = false;
}
this._hideRequestResponse = false;
this._context = null;
this._resumeStepsWaitCallback = new WaitCallback(this.ResumeStepsWaitCallback);
//判断是集成模式还是经典模式分别初始化不同的stepmanager
if (HttpRuntime.UseIntegratedPipeline)
{
this._stepManager = new HttpApplication.PipelineStepManager(this);
}
else
{
this._stepManager = new HttpApplication.ApplicationStepManager(this);
}
//创建步骤
this._stepManager.BuildSteps(this._resumeStepsWaitCallback);
}
finally
{
this._initInternalCompleted = true;
context.ConfigurationPath = null;
this._initContext.ApplicationInstance = null;
this._initContext = null;
}
}
catch
{
throw;
}
}
创建步骤及执行步骤
经典模式PipelineStepManager管道创建步骤
internal override void BuildSteps(WaitCallback stepCallback)
{
//加入步骤各个事件
ArrayList arrayList = new ArrayList();
HttpApplication application = this._application;
UrlMappingsSection urlMappings = RuntimeConfig.GetConfig().UrlMappings;
bool flag = urlMappings.IsEnabled && urlMappings.UrlMappings.Count > ;
arrayList.Add(new HttpApplication.ValidateRequestExecutionStep(application));
arrayList.Add(new HttpApplication.ValidatePathExecutionStep(application));
if (flag)
{
arrayList.Add(new HttpApplication.UrlMappingsExecutionStep(application));
}
application.CreateEventExecutionSteps(HttpApplication.EventBeginRequest, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventAuthenticateRequest, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventDefaultAuthentication, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventPostAuthenticateRequest, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventAuthorizeRequest, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventPostAuthorizeRequest, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventResolveRequestCache, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventPostResolveRequestCache, arrayList);
arrayList.Add(new HttpApplication.MapHandlerExecutionStep(application));
application.CreateEventExecutionSteps(HttpApplication.EventPostMapRequestHandler, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventAcquireRequestState, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventPostAcquireRequestState, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventPreRequestHandlerExecute, arrayList);
arrayList.Add(application.CreateImplicitAsyncPreloadExecutionStep());
//加入mvchandler执行的步骤事件
arrayList.Add(new HttpApplication.CallHandlerExecutionStep(application));
application.CreateEventExecutionSteps(HttpApplication.EventPostRequestHandlerExecute, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventReleaseRequestState, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventPostReleaseRequestState, arrayList);
arrayList.Add(new HttpApplication.CallFilterExecutionStep(application));
application.CreateEventExecutionSteps(HttpApplication.EventUpdateRequestCache, arrayList);
application.CreateEventExecutionSteps(HttpApplication.EventPostUpdateRequestCache, arrayList);
this._endRequestStepIndex = arrayList.Count;
application.CreateEventExecutionSteps(HttpApplication.EventEndRequest, arrayList);
arrayList.Add(new HttpApplication.NoopExecutionStep());
this._execSteps = new HttpApplication.IExecutionStep[arrayList.Count];
arrayList.CopyTo(this._execSteps);
this._resumeStepsWaitCallback = stepCallback;
}
CallHandlerExecutionStep
void HttpApplication.IExecutionStep.Execute()
{
HttpContext context = this._application.Context;
IHttpHandler handler = context.Handler;
if (EtwTrace.IsTraceEnabled(4, 4))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_ENTER, context.WorkerRequest);
}
if (handler != null && HttpRuntime.UseIntegratedPipeline)
{
IIS7WorkerRequest iIS7WorkerRequest = context.WorkerRequest as IIS7WorkerRequest;
if (iIS7WorkerRequest != null && iIS7WorkerRequest.IsHandlerExecutionDenied())
{
this._sync = true;
HttpException ex = new HttpException(403, SR.GetString("Handler_access_denied"));
ex.SetFormatter(new PageForbiddenErrorFormatter(context.Request.Path, SR.GetString("Handler_access_denied")));
throw ex;
}
}
if (handler == null)
{
this._sync = true;
return;
}
if (handler is IHttpAsyncHandler)
{
IHttpAsyncHandler httpAsyncHandler = (IHttpAsyncHandler)handler;
this._sync = false;
this._handler = httpAsyncHandler;
Func<HttpContext, AsyncCallback, object, IAsyncResult> func = AppVerifier.WrapBeginMethod<HttpContext>(this._application, new Func<HttpContext, AsyncCallback, object, IAsyncResult>(httpAsyncHandler.BeginProcessRequest));
this._asyncStepCompletionInfo.Reset();
context.SyncContext.AllowVoidAsyncOperations();
IAsyncResult asyncResult;
try
{
//执行beginprocessrequest方法
asyncResult = func(context, this._completionCallback, null);
}
catch
{
context.SyncContext.ProhibitVoidAsyncOperations();
throw;
}
bool flag;
bool flag2;
this._asyncStepCompletionInfo.RegisterBeginUnwound(asyncResult, out flag, out flag2);
if (flag)
{
this._sync = true;
this._handler = null;
context.SyncContext.ProhibitVoidAsyncOperations();
try
{
if (flag2)
{
httpAsyncHandler.EndProcessRequest(asyncResult);
}
this._asyncStepCompletionInfo.ReportError();
}
finally
{
HttpApplication.CallHandlerExecutionStep.SuppressPostEndRequestIfNecessary(context);
context.Response.GenerateResponseHeadersForHandler();
}
if (EtwTrace.IsTraceEnabled(4, 4))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_LEAVE, context.WorkerRequest);
return;
}
}
}
else
{
this._sync = true;
context.SyncContext.SetSyncCaller();
try
{
handler.ProcessRequest(context);
}
finally
{
context.SyncContext.ResetSyncCaller();
if (EtwTrace.IsTraceEnabled(4, 4))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_LEAVE, context.WorkerRequest);
}
HttpApplication.CallHandlerExecutionStep.SuppressPostEndRequestIfNecessary(context);
context.Response.GenerateResponseHeadersForHandler();
}
}
}
执行步骤
internal override void ResumeSteps(Exception error)
{
bool flag = false;
bool flag2 = true;
HttpApplication application = this._application;
CountdownTask applicationInstanceConsumersCounter = application.ApplicationInstanceConsumersCounter;
HttpContext context = application.Context;
ThreadContext threadContext = null;
AspNetSynchronizationContextBase syncContext = context.SyncContext;
try
{
if (applicationInstanceConsumersCounter != null)
{
applicationInstanceConsumersCounter.MarkOperationPending();
}
using (syncContext.AcquireThreadLock())
{
try
{
threadContext = application.OnThreadEnter();
}
catch (Exception ex)
{
if (error == null)
{
error = ex;
}
}
try
{
try
{
while (true)
{
if (syncContext.Error != null)
{
error = syncContext.Error;
syncContext.ClearError();
}
if (error != null)
{
application.RecordError(error);
error = null;
}
if (syncContext.PendingCompletion(this._resumeStepsWaitCallback))
{
goto IL_123;
}
if (this._currentStepIndex < this._endRequestStepIndex && (context.Error != null || this._requestCompleted))
{
context.Response.FilterOutput();
this._currentStepIndex = this._endRequestStepIndex;
}
else
{
this._currentStepIndex++;
}
if (this._currentStepIndex >= this._execSteps.Length)
{
break;
}
this._numStepCalls++;
syncContext.Enable();
//执行每个事件中的Excute方法
error = application.ExecuteStep(this._execSteps[this._currentStepIndex], ref flag2);
if (!flag2)
{
goto IL_123;
}
this._numSyncStepCalls++;
}
flag = true;
IL_123:;
}
finally
{
if (flag)
{
context.RaiseOnRequestCompleted();
}
if (threadContext != null)
{
try
{
threadContext.DisassociateFromCurrentThread();
}
catch
{
}
}
}
}
catch
{
throw;
}
}
if (flag)
{
context.RaiseOnPipelineCompleted();
context.Unroot();
application.AsyncResult.Complete(this._numStepCalls == this._numSyncStepCalls, null, null);
application.ReleaseAppInstance();
}
}
finally
{
if (applicationInstanceConsumersCounter != null)
{
applicationInstanceConsumersCounter.MarkOperationCompleted();
}
}
}
集成模式同理
mvc5 源码解析2-2 mvchandler的执行的更多相关文章
- mvc5 源码解析2-1:mvchandler的执行
上一节说在urlroutingmodule中mvchandler 映射到httpcontext上,那mvchandler又是怎么执行的呢? (1).httpruntime 从isapiruntime ...
- [源码解析]Oozie来龙去脉之内部执行
[源码解析]Oozie来龙去脉之内部执行 目录 [源码解析]Oozie来龙去脉之内部执行 0x00 摘要 0x01 Oozie阶段 1.1 ActionStartXCommand 1.2 HiveAc ...
- mvc5 源码解析1:UrlRoutingModule
注册在C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG \webconfig中 在该module源码中 我们可以看出注册了application ...
- 【JVM源码解析】模板解释器解释执行Java字节码指令(上)
本文由HeapDump性能社区首席讲师鸠摩(马智)授权整理发布 第17章-x86-64寄存器 不同的CPU都能够解释的机器语言的体系称为指令集架构(ISA,Instruction Set Archit ...
- Celery 源码解析三: Task 对象的实现
Task 的实现在 Celery 中你会发现有两处,一处位于 celery/app/task.py,这是第一个:第二个位于 celery/task/base.py 中,这是第二个.他们之间是有关系的, ...
- Celery 源码解析五: 远程控制管理
今天要聊的话题可能被大家关注得不过,但是对于 Celery 来说确实很有用的功能,曾经我在工作中遇到这类情况,就是我们将所有的任务都放在同一个队列里面,然后有一天突然某个同学的代码写得不对,导致大量的 ...
- Celery 源码解析六:Events 的实现
在 Celery 中,除了远程控制之外,还有一个元素可以让我们对分布式中的任务的状态有所掌控,而且从实际意义上来说,这个元素对 Celery 更为重要,这就是在本文中将要说到的 Event. 在 Ce ...
- 6 admin(注册设计)源码解析、单例模式
1.单例模式 https://www.cnblogs.com/yuanchenqi/articles/8323452.html 单例模式(Singleton Pattern)是一种常用的软件设计模式, ...
- [源码解析] 并行分布式框架 Celery 之 worker 启动 (1)
[源码解析] 并行分布式框架 Celery 之 worker 启动 (1) 目录 [源码解析] 并行分布式框架 Celery 之 worker 启动 (1) 0x00 摘要 0x01 Celery的架 ...
随机推荐
- 网络远程唤醒 WOL Magic Packet【转】
转自:https://www.cnblogs.com/dcb3688/p/4608063.html Magic Packet Magic Packet白皮书介绍: The basic technica ...
- 201871010112-梁丽珍《面向对象程序设计(java)》第十四周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- 【Spring AOP】AOP介绍(一)
AOP(Aspect Oriented Programming) 面向切面编程,是Spring框架的一个重要组件. AOP应该算是对OOP(面向对象编程)的补充和完善.OOP引入封装.继承.多态等概念 ...
- Nginx虚拟主机配置(三)
虚拟主机就是使用特殊的软硬件技术,把一台计算机主机分成多台“虚拟”的主机,每一台虚拟主机都具有独立的域名和IP地址(或共享的IP地址),具有完整的Internet服务器功能.在同一台硬件.同一个操作系 ...
- JDOJ 1790: 高精度A-B
JDOJ 1790: 高精度A-B JDOJ传送门 洛谷 P2142 高精度减法 洛谷传送门 题目描述 高精度减法 输入格式 两个整数a,b(第二个可能比第一个大) 输出格式 结果(是负数要输出负号) ...
- OpenStack Nova
OpenStack Nova 简介 OpenStack 中的 Nova 负责维护和管理云环境的计算资源 Nova 在现有 Linux 服务器上作为一组守护线程来提供服务 Nova 由多个服务器进程组成 ...
- openlayers在底图上添加静态icon
越学习openlayer你会发现openlayer是真的很强大,今天记录一下学习的成果,需求是做那种室内的CAD的场景然后里面展示人员icon并且实时展示人员的位置信息,以及点击弹出对应人员的一些位置 ...
- 【转】为什么要重写hashcode()方法和toString()方法
Object 类 包含toString()和hashCode()方法. 一.toString(): 在Object类里面定义toString()方法的时候返回的对象的哈希code码,这个hashcod ...
- 数论问题(1) : poj 1061
最近,本人发现了一个新网站poj(不算新) 当然了,上面的资源很好...... 就是还没搞清楚它的搜索该怎么弄,如果有大佬能教教我怎么弄,请在下方留言 闲话少说,回归我们的正题 题目转自poj 106 ...
- python前后台tcp/udp通讯示例
以下代码兼容python2.7+.python3 TCP示例 服务器 -- sever_tcp.py #!/usr/bin/env python #coding=utf-8 import time i ...