ASP.NET Application生命周期概括(二)

下面讨论一下 MapHandlerExecutionStep和 CallHandlerExecutionStep

 CallHandlerExecutionStep作用很简单,就是去调用通过MapHandlerExecutionStep找到的HttpHandler

 

internal class CallHandlerExecutionStep : HttpApplication.IExecutionStep
{
// Fields
private HttpApplication _application;
private AsyncCallback _completionCallback;
private IHttpAsyncHandler _handler;
private bool _sync;

// Methods
internal CallHandlerExecutionStep(HttpApplication app)
{
this._application = app;
this._completionCallback = new AsyncCallback(this.OnAsyncHandlerCompletion);
}

private void OnAsyncHandlerCompletion(IAsyncResult ar)
{
if (!ar.CompletedSynchronously)
{
HttpContext context = this._application.Context;
Exception error = null;
try
{
try
{
this._handler.EndProcessRequest(ar);
}
finally
{
context.Response.GenerateResponseHeadersForHandler();
}
}
catch (Exception exception2)
{
if ((exception2 is ThreadAbortException) || ((exception2.InnerException != null) && (exception2.InnerException is ThreadAbortException)))
{
this._application.CompleteRequest();
}
else
{
error = exception2;
}
}
if (EtwTrace.IsTraceEnabled(4, 4))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_LEAVE, context.WorkerRequest);
}
this._handler = null;
context.SetStartTime();
if (HttpRuntime.IsLegacyCas)
{
this.ResumeStepsWithAssert(error);
}
else
{
this.ResumeSteps(error);
}
}
}

private void ResumeSteps(Exception error)
{
this._application.ResumeStepsFromThreadPoolThread(error);
}

[PermissionSet(SecurityAction.Assert, Unrestricted=true)]
private void ResumeStepsWithAssert(Exception error)
{
this.ResumeSteps(error);
}

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 workerRequest = context.WorkerRequest as IIS7WorkerRequest;
if ((workerRequest != null) && workerRequest.IsHandlerExecutionDenied())
{
this._sync = true;
HttpException exception = new HttpException(0x193, SR.GetString("Handler_access_denied"));
exception.SetFormatter(new PageForbiddenErrorFormatter(context.Request.Path, SR.GetString("Handler_access_denied")));
throw exception;
}
}
if (handler == null)
{
this._sync = true;
}
else if (handler is IHttpAsyncHandler)
{
IHttpAsyncHandler handler2 = (IHttpAsyncHandler) handler;
this._sync = false;
this._handler = handler2;
IAsyncResult result = handler2.BeginProcessRequest(context, this._completionCallback, null);
if (result.CompletedSynchronously)
{
this._sync = true;
this._handler = null;
try
{
handler2.EndProcessRequest(result);
}
finally
{
context.Response.GenerateResponseHeadersForHandler();
}
if (EtwTrace.IsTraceEnabled(4, 4))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_LEAVE, context.WorkerRequest);
}
}
}
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);
}
context.Response.GenerateResponseHeadersForHandler();
}
}
}

// Properties
bool HttpApplication.IExecutionStep.CompletedSynchronously
{
get
{
return this._sync;
}
}

bool HttpApplication.IExecutionStep.IsCancellable
{
get
{
return !(this._application.Context.Handler is IHttpAsyncHandler);
}
}
}

 

 

MapHandleExecutionStep的作用就是根据请求去寻找相应的HttpHandler

internal class MapHandlerExecutionStep : HttpApplication.IExecutionStep
{
// Fields
private HttpApplication _application;

// Methods
internal MapHandlerExecutionStep(HttpApplication app)
{
this._application = app;
}

void HttpApplication.IExecutionStep.Execute()
{
HttpContext context = this._application.Context;
HttpRequest request = context.Request;
if (EtwTrace.IsTraceEnabled(5, 1))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_ENTER, context.WorkerRequest);
}
context.Handler = this._application.MapHttpHandler(context, request.RequestType, request.FilePathObject, request.PhysicalPathInternal, false);
if (EtwTrace.IsTraceEnabled(5, 1))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_LEAVE, context.WorkerRequest);
}
}

// Properties
bool HttpApplication.IExecutionStep.CompletedSynchronously
{
get
{
return true;
}
}

bool HttpApplication.IExecutionStep.IsCancellable
{
get
{
return false;
}
}
}

 

internal IHttpHandler MapHttpHandler(HttpContext context, string requestType, VirtualPath path, string pathTranslated, bool useAppConfig)
{
IHttpHandler handler = (context.ServerExecuteDepth == 0) ? context.RemapHandlerInstance : null;
using (new ApplicationImpersonationContext())
{
if (handler != null)
{
return handler;
}
HttpHandlerAction mapping = this.GetHandlerMapping(context, requestType, path, useAppConfig);
if (mapping == null)
{
PerfCounters.IncrementCounter(AppPerfCounter.REQUESTS_NOT_FOUND);
PerfCounters.IncrementCounter(AppPerfCounter.REQUESTS_FAILED);
throw new HttpException(SR.GetString("Http_handler_not_found_for_request_type", new object[] { requestType }));
}
IHttpHandlerFactory factory = this.GetFactory(mapping);
try
{
IHttpHandlerFactory2 factory2 = factory as IHttpHandlerFactory2;
if (factory2 != null)
{
handler = factory2.GetHandler(context, requestType, path, pathTranslated);
}
else
{
handler = factory.GetHandler(context, requestType, path.VirtualPathString, pathTranslated);
}
}
catch (FileNotFoundException exception)
{
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
{
throw new HttpException(0x194, null, exception);
}
throw new HttpException(0x194, null);
}
catch (DirectoryNotFoundException exception2)
{
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
{
throw new HttpException(0x194, null, exception2);
}
throw new HttpException(0x194, null);
}
catch (PathTooLongException exception3)
{
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
{
throw new HttpException(0x19e, null, exception3);
}
throw new HttpException(0x19e, null);
}
if (this._handlerRecycleList == null)
{
this._handlerRecycleList = new ArrayList();
}
this._handlerRecycleList.Add(new HandlerWithFactory(handler, factory));
}
return handler;
}

 

private IHttpHandlerFactory GetFactory(HttpHandlerAction mapping)
{
HandlerFactoryCache cache = (HandlerFactoryCache) this._handlerFactories[mapping.Type];
if (cache == null)
{
cache = new HandlerFactoryCache(mapping);
this._handlerFactories[mapping.Type] = cache;
}
return cache.Factory;
}
internal HandlerFactoryCache(HttpHandlerAction mapping)
{
object obj2 = mapping.Create();
if (obj2 is IHttpHandler)
{
this._factory = new HandlerFactoryWrapper((IHttpHandler) obj2, this.GetHandlerType(mapping));
}
else
{
if (!(obj2 is IHttpHandlerFactory))
{
throw new HttpException(SR.GetString("Type_not_factory_or_handler", new object[] { obj2.GetType().FullName }));
}
this._factory = (IHttpHandlerFactory) obj2;
}
}

 

    internal object Create()
{
if (this._type == null)
{
Type t = ConfigUtil.GetType(this.Type, "type", this);
if (!ConfigUtil.IsTypeHandlerOrFactory(t))
{
throw new ConfigurationErrorsException(SR.GetString("Type_not_factory_or_handler", new object[] { this.Type }), base.ElementInformation.Source, base.ElementInformation.LineNumber);
}
this._type = t;
}
return HttpRuntime.CreateNonPublicInstance(this._type);
}

 

至于HttpHandler的映射过程,可以参考下面这篇文章

《细说 HttpHandler 的映射过程》
http://kb.cnblogs.com/page/129505/

 

转载于:https://www.cnblogs.com/edward44444/archive/2012/04/03/2431465.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值