mvc5 源码解析2-2 mvchandler的执行
2024-09-03 16:36:05
我们从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();
}
}
}
集成模式同理
最新文章
- UVA - 1625 Color Length[序列DP 代价计算技巧]
- VS2012中,C# 配置文件读取 + C#多个工程共享共有变量 + 整理using语句
- [New Portal]Windows Azure Virtual Machine (22) 使用Azure PowerShell,设置Virtual Machine Endpoint
- ORA-22868: 具有 LOB 的表包含有位于不同表空间的段
- HDU 3069 (树形DP)
- php--城市分类
- 使用jsoup进行网页内容抓取
- 仿酷狗音乐播放器开发日志二十六 duilib在标题栏弹出菜单的方法
- 1、solr 查询
- SecureCRT学习之道:用SecureCRT来上传和下载数据
- Winform美化MessageBox
- <;算法图解>;读书笔记:第2章 选择排序
- PC端的软件端口和adb 5037端口冲突解决方案
- Play XML Entities
- (4.22)sql server视图/索引视图概念
- [UE4]GameInstance初始化
- python中,获取字符串的长度
- javaScript 之set/get方法的使用
- Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结
- Windows降权