本章节主要讲IIS和Asp.Net 管道内部如何处理客户端Http请求,会较多的以代码的形式讲述,让大家对HttpApplication、HttpHandler、HttpApplicationFactory、Page这几个在处理请求过程中扮演重要角色的对象有更深入的了解。
我们都知道,当用户在浏览器地址栏中输入网址时,该请求会被IIS服务器捕获,如果是请求的是静态页面则由IIS本身处理并直接返回客户端,如果是动态页(*.aspx),通过一序列的前期(这部分处理过程本文不详细讲解,可以参考其他文章)的处理来到.NET运行时,然后交给Aspnet_ispai.dll处理,处理完成后返回处理结果。请求和处理过程可以分为:HttpRuntime->HttpModule->HtppApplicationFactory->HttpApplication->HttpHandlerFactory->HttpHandler->HttpModule->将请求结果返回到客户端。
下面我们通过单步调式跟踪System.Web.Dll源码来分析各个过程中关键处理函数(关于如何单步调式.Net FrameWork 源码我将在后面的文章中给出)
(说明:部分代码只保留了关键代码)
1、首先进入管道运行时中的托管入口函数
IIS集成模式:
在IPipelineRuntime.cs类中请求通知函数:ProcessRequestNotification,并调用ProcessRequestNotificationHelper函数
-
internal static int ProcessRequestNotification(
-
IntPtr rootedObjectsPointer,
-
IntPtr nativeRequestContext,
-
-
-
-
-
-
return ProcessRequestNotificationHelper(rootedObjectsPointer, nativeRequestContext, moduleData, flags);
-
-
-
ApplicationManager.RecordFatalException(e);
-
-
-
2、在ProcessRequestNotificationHelper函数中调用运行时HttpRuntime中的ProcessRequestNotification函数
-
internal static int ProcessRequestNotificationHelper(
-
IntPtr rootedObjectsPointer,
-
IntPtr nativeRequestContext,
-
-
-
-
IIS7WorkerRequest wr =
null;
-
HttpContext context =
null;
-
RequestNotificationStatus status = RequestNotificationStatus.Continue;
-
-
bool workerRequestWasJustCreated =
false;
-
-
-
-
status = HttpRuntime.ProcessRequestNotification(wr, context);
-
-
-
-
-
-
-
3、在ProcessRequestNotification中调用ProcessRequestNotificationPrivate
4、处理请求通知ProcessRequestNotificationPrivate函数中调用了HttpApplicatioinFactory的GetApplicationInstance()函数来获取HttpApplication实例对象,并调用BeginProcessRequestNotification处理请求
-
private RequestNotificationStatus ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) {
-
RequestNotificationStatus status = RequestNotificationStatus.Pending;
-
-
-
-
IHttpHandler handler =
null;
-
if (context.NeedToInitializeApp()) {
-
-
-
EnsureFirstRequestInit(context);
-
-
-
-
-
-
context.Response.InitResponseWriter();
-
handler = HttpApplicationFactory.GetApplicationInstance(context);
-
-
throw
new HttpException(SR.GetString(SR.Unable_create_app_object));
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, context.WorkerRequest, handler.GetType().FullName,
"Start");
-
-
HttpApplication app = handler
as HttpApplication;
-
-
-
app.AssignContext(context);
-
-
-
-
-
wr.SynchronizeVariables(context);
-
-
if (context.ApplicationInstance !=
null) {
-
-
IAsyncResult ar = context.ApplicationInstance.BeginProcessRequestNotification(context, _requestNotificationCompletionCallback);
-
-
if (ar.CompletedSynchronously) {
-
status = RequestNotificationStatus.Continue;
-
-
-
else
if (handler !=
null) {
-
-
handler.ProcessRequest(context);
-
status = RequestNotificationStatus.FinishRequest;
-
-
-
status = RequestNotificationStatus.Continue;
-
-
-
-
-
-
-
IIS经典模式:
在ISAPIRuntime.cs类中请求 ProcessRequest函数并调用HttpRuntime.ProcessReques方法
1、HttpRuntime处理请求入口函数
-
public static void ProcessRequest(HttpWorkerRequest wr) {
-
-
throw
new ArgumentNullException(
"wr");
-
-
if (HttpRuntime.UseIntegratedPipeline) {
-
throw
new PlatformNotSupportedException(SR.GetString(SR.Method_Not_Supported_By_Iis_Integrated_Mode,
"HttpRuntime.ProcessRequest"));
-
-
-
ProcessRequestNoDemand(wr);
-
2、ProcessRequestNoDemand函数,调用ProcessRequestNow函数
-
internal static void ProcessRequestNoDemand(HttpWorkerRequest wr) {
-
RequestQueue rq = _theRuntime._requestQueue;
-
-
wr.UpdateInitialCounters();
-
-
-
wr = rq.GetRequestToExecute(wr);
-
-
-
CalculateWaitTimeAndUpdatePerfCounter(wr);
-
-
ProcessRequestNow(wr);调用ProcessRequestNow
-
-
3、ProcessRequestNow函数,调用ProcessRequestInternal
-
internal static void ProcessRequestNow(HttpWorkerRequest wr)
-
-
_theRuntime.ProcessRequestInternal(wr);
-
4、ProcessRequestInternal函数,调用HttpApplicatioinFactory的GetApplicationInstance()函数来获取HttpApplication实例对象
-
private void ProcessRequestInternal(HttpWorkerRequest wr) {
-
-
Interlocked.Increment(
ref _activeRequestCount);
-
-
-
-
IHttpHandler app = HttpApplicationFactory.GetApplicationInstance(context);
-
-
-
throw
new HttpException(SR.GetString(SR.Unable_create_app_object));
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, context.WorkerRequest, app.GetType().FullName,
"Start");
-
-
if (app
is IHttpAsyncHandler) {
-
-
IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler)app;
-
context.AsyncAppHandler = asyncHandler;
-
-
asyncHandler.BeginProcessRequest(context, _handlerCompletionCallback, context);
-
-
-
-
app.ProcessRequest(context);
-
FinishRequest(context.WorkerRequest, context,
null);
-
-
-
-
context.Response.InitResponseWriter();
-
FinishRequest(wr, context, e);
-
-
说明:以上4个步骤集成模式和经典模式分不同的函数执行,从下面开始两种模式调用相同的函数,只是在函数中针对不同的模式进行不同的处理
5、进入HttpApplicationFactory,调用GetNormalApplicationInstance函数
-
internal static IHttpHandler GetApplicationInstance(HttpContext context) {
-
if (_customApplication !=
null)
-
return _customApplication;
-
-
if (context.Request.IsDebuggingRequest)
-
return
new HttpDebugHandler();
-
-
_theApplicationFactory.EnsureInited();
-
-
_theApplicationFactory.EnsureAppStartCalled(context);
-
-
return _theApplicationFactory.GetNormalApplicationInstance(context);
-
-
6、进入GetNormalApplicationInstance函数,调用InitInternal初始化HttpApplication内部对象
-
private HttpApplication GetNormalApplicationInstance(HttpContext context) {
-
HttpApplication app =
null;
-
-
-
if (_numFreeAppInstances >
0) {
-
app = (HttpApplication)_freeList.Pop();
-
-
-
if (_numFreeAppInstances < _minFreeAppInstances) {
-
_minFreeAppInstances = _numFreeAppInstances;
-
-
-
-
-
-
-
app = (HttpApplication)HttpRuntime.CreateNonPublicInstance(_theApplicationType);
-
-
using (
new ApplicationImpersonationContext()) {
-
app.InitInternal(context, _state, _eventHandlerMethods);
-
-
-
-
if (AppSettings.UseTaskFriendlySynchronizationContext) {
-
-
app.ApplicationInstanceConsumersCounter =
new CountdownTask(
1);
-
app.ApplicationInstanceConsumersCounter.Task.ContinueWith((_, o) => RecycleApplicationInstance((HttpApplication)o), app, TaskContinuationOptions.ExecuteSynchronously);
-
-
-
7、进入HttpApplication,调用InitInternal函数来初始化Application内部对象。初始化HttpModule和建立处理执行步骤
-
internal void InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers) {
-
Debug.Assert(context !=
null,
"context != null");
-
-
-
-
-
PerfCounters.IncrementCounter(AppPerfCounter.PIPELINES);
-
-
-
-
-
-
_initContext.ApplicationInstance =
this;
-
-
-
context.ConfigurationPath = context.Request.ApplicationPathObject;
-
-
-
using (
new DisposableHttpContextWrapper(context)) {
-
-
-
if (HttpRuntime.UseIntegratedPipeline) {
-
-
Debug.Assert(_moduleConfigInfo !=
null,
"_moduleConfigInfo != null");
-
Debug.Assert(_moduleConfigInfo.Count >=
0,
"_moduleConfigInfo.Count >= 0");
-
-
-
context.HideRequestResponse =
true;
-
_hideRequestResponse =
true;
-
-
-
-
context.HideRequestResponse =
false;
-
_hideRequestResponse =
false;
-
-
-
-
-
-
-
Debug.Assert(
null == _moduleContainers,
"null == _moduleContainers");
-
-
-
-
-
HookupEventHandlersForApplicationAndModules(handlers);
-
-
-
-
if (HttpRuntime.UseIntegratedPipeline && _context !=
null) {
-
_context.HideRequestResponse =
true;
-
-
_hideRequestResponse =
true;
-
-
-
-
-
-
-
-
-
-
if (HttpRuntime.UseIntegratedPipeline && _context !=
null) {
-
_context.HideRequestResponse =
false;
-
-
_hideRequestResponse =
false;
-
-
_resumeStepsWaitCallback=
new WaitCallback(
this.ResumeStepsWaitCallback);
-
-
-
"color:#FF0000;">
if (HttpRuntime.UseIntegratedPipeline) {
-
"color:#FF0000;">_stepManager =
new PipelineStepManager(
this);
-
-
-
"color:#FF0000;"> _stepManager =
new ApplicationStepManager(
this);
-
-
-
"color:#FF0000;">_stepManager.BuildSteps(_resumeStepsWaitCallback);
-
-
-
_initInternalCompleted =
true;
-
-
-
context.ConfigurationPath =
null;
-
-
-
_initContext.ApplicationInstance =
null;
-
-
-
-
-
-
-
8、初始化HttpModule,包含系统默认的HttpModule和自定义的HttpModule
1)、系统默认的HttpModule:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config
2)、自定义的HttpModule在网站的Web.config中配置
经典模式:
-
-
-
<
add name=
"mymodule" type=
"WebApplication2.myModule"/>
-
-
集成模式:
-
-
-
<
add name=
"mymodule" type=
"WebApplication2.myModule"/>
-
-
3)、经典模式初始化:
-
private void InitModules() {
-
-
-
HttpModulesSection pconfig = RuntimeConfig.GetAppConfig().HttpModules;
-
-
-
HttpModuleCollection moduleCollection = pconfig.CreateModules();
-
HttpModuleCollection dynamicModules = CreateDynamicModules();
-
-
moduleCollection.AppendCollection(dynamicModules);
-
_moduleCollection = moduleCollection;
-
-
-
4)、初始化各个HttpModule对象.
-
private void InitModulesCommon() {
-
int n = _moduleCollection.Count;
-
-
for (
int i =
0; i < n; i++) {
-
-
-
_currentModuleCollectionKey = _moduleCollection.GetKey(i);
-
_moduleCollection[i].Init(
this);
-
-
-
_currentModuleCollectionKey =
null;
-
-
5)、初始化HttpModule,在Init函数中注册HttpApplication对象的事件,这里举了两个例子
OutputCacheModule.cs web.config中默认第一个HttpModule
-
void IHttpModule.Init(HttpApplication app) {
-
OutputCacheSection cacheConfig = RuntimeConfig.GetAppConfig().OutputCache;
-
if (cacheConfig.EnableOutputCache) {
-
app.ResolveRequestCache +=
new EventHandler(
this.OnEnter);
-
app.UpdateRequestCache +=
new EventHandler(
this.OnLeave);
-
-
SessionStateModule.cs web.config中默认第二个HttpModule,实现Session的系统HttpModule
-
public void Init(HttpApplication app) {
-
bool initModuleCalled =
false;
-
SessionStateSection config = RuntimeConfig.GetAppConfig().SessionState;
-
-
-
s_lock.AcquireWriterLock();
-
-
-
InitModuleFromConfig(app, config);
-
-
-
if (!CheckTrustLevel(config))
-
s_trustLevelInsufficient =
true;
-
-
s_timeout = (
int)config.Timeout.TotalMinutes;
-
-
s_useHostingIdentity = config.UseHostingIdentity;
-
-
-
-
if (config.Mode == SessionStateMode.InProc &&
-
_usingAspnetSessionIdManager) {
-
s_allowInProcOptimization =
true;
-
-
-
if (config.Mode != SessionStateMode.Custom &&
-
config.Mode != SessionStateMode.Off &&
-
!config.RegenerateExpiredSessionId) {
-
s_allowDelayedStateStoreItemCreation =
true;
-
-
-
s_configExecutionTimeout = RuntimeConfig.GetConfig().HttpRuntime.ExecutionTimeout;
-
-
s_configRegenerateExpiredSessionId = config.RegenerateExpiredSessionId;
-
s_configCookieless = config.Cookieless;
-
s_configMode = config.Mode;
-
-
-
-
-
Debug.Trace(
"SessionStateModuleInit",
-
"Configuration: _mode=" + config.Mode +
-
";Timeout=" + config.Timeout +
-
";CookieMode=" + config.Cookieless +
-
";SqlConnectionString=" + config.SqlConnectionString +
-
";StateConnectionString=" + config.StateConnectionString +
-
";s_allowInProcOptimization=" + s_allowInProcOptimization +
-
";s_allowDelayedStateStoreItemCreation=" + s_allowDelayedStateStoreItemCreation);
-
-
-
-
-
s_lock.ReleaseWriterLock();
-
-
-
-
-
InitModuleFromConfig(app, config);
-
-
-
if (s_trustLevelInsufficient) {
-
throw
new HttpException(SR.GetString(SR.Session_state_need_higher_trust));
-
-
集成模式初始化HttpModule代码我就不贴出来了,大家可以在源码中查看
9、建立处理步骤BuildSteps,将所有要执行的步骤载入到一个IExecutionStep[]集合中,待后面ResumeSteps调用
经典模式:调用ApplicationStepManager类中的BuildSteps函数
-
internal
class
ApplicationStepManager :
StepManager {
-
private IExecutionStep[] _execSteps;
-
private WaitCallback _resumeStepsWaitCallback;
-
private
int _currentStepIndex;
-
private
int _numStepCalls;
-
private
int _numSyncStepCalls;
-
private
int _endRequestStepIndex;
-
-
internal ApplicationStepManager(HttpApplication app): base(app) {
-
-
-
internal override void BuildSteps(WaitCallback stepCallback )
-
-
ArrayList steps =
new ArrayList();
-
HttpApplication app = _application;
-
-
bool urlMappingsEnabled =
false;
-
UrlMappingsSection urlMappings = RuntimeConfig.GetConfig().UrlMappings;
-
urlMappingsEnabled = urlMappings.IsEnabled && ( urlMappings.UrlMappings.Count >
0 );
-
-
steps.Add(
new ValidateRequestExecutionStep(app));
-
steps.Add(
new ValidatePathExecutionStep(app));
-
-
-
steps.Add(
new UrlMappingsExecutionStep(app));
-
-
app.CreateEventExecutionSteps(HttpApplication.EventBeginRequest, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventAuthenticateRequest, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventDefaultAuthentication, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventPostAuthenticateRequest, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventAuthorizeRequest, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventPostAuthorizeRequest, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventResolveRequestCache, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventPostResolveRequestCache, steps);
-
steps.Add(
new MapHandlerExecutionStep(app));
-
app.CreateEventExecutionSteps(HttpApplication.EventPostMapRequestHandler, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventAcquireRequestState, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventPostAcquireRequestState, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventPreRequestHandlerExecute, steps);
-
steps.Add(app.CreateImplicitAsyncPreloadExecutionStep());
-
steps.Add(
new CallHandlerExecutionStep(app));
-
app.CreateEventExecutionSteps(HttpApplication.EventPostRequestHandlerExecute, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventReleaseRequestState, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventPostReleaseRequestState, steps);
-
steps.Add(
new CallFilterExecutionStep(app));
-
app.CreateEventExecutionSteps(HttpApplication.EventUpdateRequestCache, steps);
-
app.CreateEventExecutionSteps(HttpApplication.EventPostUpdateRequestCache, steps);
-
_endRequestStepIndex = steps.Count;
-
app.CreateEventExecutionSteps(HttpApplication.EventEndRequest, steps);
-
steps.Add(
new NoopExecutionStep());
-
-
_execSteps =
new IExecutionStep[steps.Count];
-
steps.CopyTo(_execSteps);
-
-
-
_resumeStepsWaitCallback = stepCallback;
-
-
集成模式:调用PipelineStepManager类中的 BuildSteps函数
-
internal override void BuildSteps(WaitCallback stepCallback) {
-
Debug.Trace(
"PipelineRuntime",
"BuildSteps");
-
-
HttpApplication app = _application;
-
-
-
-
-
IExecutionStep materializeStep =
new MaterializeHandlerExecutionStep(app);
-
-
-
-
HttpApplication.IMPLICIT_HANDLER,
-
RequestNotification.MapRequestHandler,
-
-
-
-
-
HttpApplication.IMPLICIT_HANDLER,
-
RequestNotification.ExecuteRequestHandler,
-
false, app.CreateImplicitAsyncPreloadExecutionStep());
-
-
-
IExecutionStep handlerStep =
new CallHandlerExecutionStep(app);
-
-
-
HttpApplication.IMPLICIT_HANDLER,
-
RequestNotification.ExecuteRequestHandler,
-
-
-
-
IExecutionStep webSocketsStep =
new TransitionToWebSocketsExecutionStep(app);
-
-
-
HttpApplication.IMPLICIT_HANDLER,
-
RequestNotification.EndRequest,
-
-
-
-
IExecutionStep filterStep =
new CallFilterExecutionStep(app);
-
-
-
-
HttpApplication.IMPLICIT_FILTER_MODULE,
-
RequestNotification.UpdateRequestCache,
-
-
-
-
-
HttpApplication.IMPLICIT_FILTER_MODULE,
-
RequestNotification.LogRequest,
-
-
-
_resumeStepsWaitCallback = stepCallback;
-
10、真正处理请求阶段, 调用ResumeSteps函数来执行处理步骤
经典模式:
返回到ProcessRequestInternal函数中执行BeginProcessRequest函数
-
IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) {
-
-
-
-
-
_context.ApplicationInstance =
this;
-
-
_stepManager.InitRequest();
-
-
-
-
-
-
result =
new HttpAsyncResult(cb, extraData);
-
-
-
-
-
if (_context.TraceIsEnabled)
-
HttpRuntime.Profile.StartRequest(_context);
-
-
-
-
-
-
-
ResumeSteps函数,调用ExecuteStepl函数来执行处理步骤
-
internal override void ResumeSteps(Exception error) {
-
bool appCompleted =
false;
-
bool stepCompletedSynchronously =
true;
-
HttpApplication app = _application;
-
CountdownTask appInstanceConsumersCounter = app.ApplicationInstanceConsumersCounter;
-
HttpContext context = app.Context;
-
ThreadContext threadContext =
null;
-
AspNetSynchronizationContextBase syncContext = context.SyncContext;
-
-
Debug.Trace(
"Async",
"HttpApplication.ResumeSteps");
-
-
-
if (appInstanceConsumersCounter !=
null) {
-
appInstanceConsumersCounter.MarkOperationPending();
-
-
-
using (syncContext.AcquireThreadLock()) {
-
-
-
error = app.ExecuteStep(_execSteps[_currentStepIndex],
ref stepCompletedSynchronously);
-
-
-
集成模式:
返回到ProcessRequestNotificationPrivate函数,执行BeginProcessRequestNotification
BeginProcessRequestNotification函数,调用ResumeSteps执行处理步骤
-
internal IAsyncResult BeginProcessRequestNotification(HttpContext context, AsyncCallback cb)
-
-
if (
this._context ==
null)
-
-
this.AssignContext(context);
-
-
context.CurrentModuleEventIndex =
-1;
-
HttpAsyncResult result =
new HttpAsyncResult(cb, context);
-
context.NotificationContext.AsyncResult = result;
-
-
-
ResumeSteps
-
-
-
-
-
[
System.Diagnostics.DebuggerStepperBoundaryAttribute]
-
internal override void ResumeSteps(Exception error) {
-
HttpContext context = _application.Context;
-
IIS7WorkerRequest wr = context.WorkerRequest
as IIS7WorkerRequest;
-
AspNetSynchronizationContextBase syncContext = context.SyncContext;
-
-
RequestNotificationStatus status = RequestNotificationStatus.Continue;
-
ThreadContext threadContext =
null;
-
bool needToDisassociateThreadContext =
false;
-
bool isSynchronousCompletion =
false;
-
bool needToComplete =
false;
-
bool stepCompletedSynchronously =
false;
-
int currentModuleLastEventIndex = _application.CurrentModuleContainer.GetEventCount(context.CurrentNotification, context.IsPostNotification) -
1;
-
CountdownTask appInstanceConsumersCounter = _application.ApplicationInstanceConsumersCounter;
-
-
using (context.RootedObjects.WithinTraceBlock()) {
-
-
error = _application.ExecuteStep(step,
ref stepCompletedSynchronously);
-
-
-
11、ExecuteStep执行BuildSteps中的各个步骤
-
internal Exception ExecuteStep(IExecutionStep step, ref bool completedSynchronously) {
-
-
-
-
-
if (step.IsCancellable) {
-
_context.BeginCancellablePeriod();
-
-
-
-
-
-
_context.EndCancellablePeriod();
-
-
-
_context.WaitForExceptionIfCancelled();
-
-
-
-
-
-
if (!step.CompletedSynchronously) {
-
completedSynchronously =
false;
-
-
-
-
-
-
这里贴出了两个很重要的步骤:获取HttpHandler(MaterializeHandlerExecutionStep)和执行HttpHandler(CallHandlerExecutionStep)
经典模式:
MapHandlerExecutionStep
-
-
internal
class <
span
style=
"color:#FF0000;">MapHandlerExecutionStep : IExecutionStep {
-
private HttpApplication _application;
-
-
internal MapHandlerExecutionStep(HttpApplication app) {
-
-
-
-
void IExecutionStep.Execute() {
-
HttpContext context = _application.Context;
-
HttpRequest request = context.Request;
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_ENTER, context.WorkerRequest);
-
-
context.Handler = _application.MapHttpHandler(
-
-
-
-
request.PhysicalPathInternal,
-
-
Debug.Assert(context.ConfigurationPath == context.Request.FilePathObject,
"context.ConfigurationPath (" +
-
context.ConfigurationPath +
") != context.Request.FilePath (" + context.Request.FilePath +
")");
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_LEAVE, context.WorkerRequest);
-
-
-
bool IExecutionStep.CompletedSynchronously {
-
-
-
-
bool IExecutionStep.IsCancellable {
-
-
-
MapHttpHandler获取处理请求的HttpHandler
-
internal IHttpHandler MapHttpHandler(HttpContext context, string requestType, VirtualPath path, string pathTranslated, bool useAppConfig)
-
-
IHttpHandler handler = (context.ServerExecuteDepth ==
0) ? context.RemapHandlerInstance :
null;
-
using (
new ApplicationImpersonationContext())
-
-
-
-
-
-
HttpHandlerAction mapping =
this.GetHandlerMapping(context, requestType, path, useAppConfig);
-
-
-
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);
-
-
-
IHttpHandlerFactory2 factory2 = factory
as IHttpHandlerFactory2;
-
-
-
handler = factory2.GetHandler(context, requestType, path, pathTranslated);
-
-
-
-
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));
-
-
-
-
集成模式:
MaterializeHandlerExecutionStep步骤
-
internal
class
MaterializeHandlerExecutionStep:
IExecutionStep {
-
private HttpApplication _application;
-
-
internal MaterializeHandlerExecutionStep(HttpApplication app) {
-
-
-
-
void IExecutionStep.Execute() {
-
HttpContext context = _application.Context;
-
HttpRequest request = context.Request;
-
IHttpHandler handler =
null;
-
string configType =
null;
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_ENTER, context.WorkerRequest);
-
-
IIS7WorkerRequest wr = context.WorkerRequest
as IIS7WorkerRequest;
-
-
-
if (context.RemapHandlerInstance !=
null){
-
-
wr.SetScriptMapForRemapHandler();
-
context.Handler = context.RemapHandlerInstance;
-
-
else
if (request.RewrittenUrl !=
null) {
-
-
-
configType = wr.ReMapHandlerAndGetHandlerTypeString(context, request.Path,
out handlerExists);
-
-
-
throw
new HttpException(
404, SR.GetString(SR.Http_handler_not_found_for_request_type, request.RequestType));
-
-
-
-
configType = wr.GetManagedHandlerType();
-
-
-
if (!String.IsNullOrEmpty(configType)) {
-
IHttpHandlerFactory factory = _application.GetFactory(configType);
-
string pathTranslated = request.PhysicalPathInternal;
-
-
-
handler = factory.GetHandler(context, request.RequestType, request.FilePath, pathTranslated);
-
-
catch (FileNotFoundException e) {
-
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
-
throw
new HttpException(
404,
null, e);
-
-
throw
new HttpException(
404,
null);
-
-
catch (DirectoryNotFoundException e) {
-
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
-
throw
new HttpException(
404,
null, e);
-
-
throw
new HttpException(
404,
null);
-
-
catch (PathTooLongException e) {
-
if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
-
throw
new HttpException(
414,
null, e);
-
-
throw
new HttpException(
414,
null);
-
-
-
context.Handler = handler;
-
-
-
if (_application._handlerRecycleList ==
null)
-
_application._handlerRecycleList =
new ArrayList();
-
_application._handlerRecycleList.Add(
new HandlerWithFactory(handler, factory));
-
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_LEAVE, context.WorkerRequest);
-
-
-
bool IExecutionStep.CompletedSynchronously {
-
-
-
-
bool IExecutionStep.IsCancellable {
-
-
-
12、执行callhandlerexecutestep步骤,调用handler.ProcessRequest(context),如果HttpHandler类型为Page,则开始Page页生命周期
-
internal
class
CallHandlerExecutionStep:
IExecutionStep {
-
private HttpApplication _application;
-
private AsyncCallback _completionCallback;
-
private IHttpAsyncHandler _handler;
-
private AsyncStepCompletionInfo _asyncStepCompletionInfo;
-
-
-
internal CallHandlerExecutionStep(HttpApplication app) {
-
-
_completionCallback =
new AsyncCallback(
this.OnAsyncHandlerCompletion);
-
-
-
voidIExecutionStep.Execute()
-
-
HttpContext context = _application.Context;
-
IHttpHandler handler = context.Handler;
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Information, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_ENTER, context.WorkerRequest);
-
-
if (handler !=
null && HttpRuntime.UseIntegratedPipeline) {
-
IIS7WorkerRequest wr = context.WorkerRequest
as IIS7WorkerRequest;
-
if (wr !=
null && wr.IsHandlerExecutionDenied()) {
-
-
HttpException error =
new HttpException(
403, SR.GetString(SR.Handler_access_denied));
-
error.SetFormatter(
new PageForbiddenErrorFormatter(context.Request.Path, SR.GetString(SR.Handler_access_denied)));
-
-
-
-
-
-
-
-
else
if (handler
is IHttpAsyncHandler) {
-
-
IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler)handler;
-
-
-
-
-
var beginProcessRequestDelegate = AppVerifier.WrapBeginMethod(_application, asyncHandler.BeginProcessRequest);
-
-
_asyncStepCompletionInfo.Reset();
-
context.SyncContext.AllowVoidAsyncOperations();
-
-
-
ar = beginProcessRequestDelegate(context, _completionCallback,
null);
-
-
-
-
-
context.SyncContext.ProhibitVoidAsyncOperations();
-
-
-
-
-
-
_asyncStepCompletionInfo.RegisterBeginUnwound(ar,
out operationCompleted,
out mustCallEndHandler);
-
-
if (operationCompleted) {
-
-
-
-
-
-
context.SyncContext.ProhibitVoidAsyncOperations();
-
-
-
if (mustCallEndHandler) {
-
asyncHandler.EndProcessRequest(ar);
-
-
-
_asyncStepCompletionInfo.ReportError();
-
-
-
SuppressPostEndRequestIfNecessary(context);
-
-
-
-
context.Response.GenerateResponseHeadersForHandler();
-
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Information, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_LEAVE, context.WorkerRequest);
-
-
-
-
-
-
-
context.SyncContext.SetSyncCaller();
-
-
-
handler.ProcessRequest(context);
-
-
-
context.SyncContext.ResetSyncCaller();
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Information, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_LEAVE, context.WorkerRequest);
-
-
SuppressPostEndRequestIfNecessary(context);
-
-
context.Response.GenerateResponseHeadersForHandler();
-
-
-
-
-
bool IExecutionStep.CompletedSynchronously {
-
-
-
-
bool IExecutionStep.IsCancellable {
-
-
get {
return (_application.Context.Handler
is IHttpAsyncHandler) ?
false :
true; }
-
-
13、进入Page类的 ProcessRequest方法开始处理请求
-
private void ProcessRequest() {
-
-
-
Thread currentThread = Thread.CurrentThread;
-
CultureInfo prevCulture = currentThread.CurrentCulture;
-
CultureInfo prevUICulture = currentThread.CurrentUICulture;
-
-
-
ProcessRequest(
true
,
true
);
-
-
-
-
RestoreCultures(currentThread, prevCulture, prevUICulture);
-
-
14、进入ProcessRequest,调用ProcessRequestMain函数
-
private void ProcessRequest(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) {
-
-
-
-
if (includeStagesBeforeAsyncPoint) {
-
-
-
this.ControlState = ControlState.FrameworkInitialized;
-
-
-
bool needToCallEndTrace = Context.WorkerRequest
is IIS7WorkerRequest;
-
-
-
-
ProcessRequestTransacted();
-
-
-
-
ProcessRequestMain(includeStagesBeforeAsyncPoint, includeStagesAfterAsyncPoint);
-
-
-
if (includeStagesAfterAsyncPoint) {
-
needToCallEndTrace =
false;
-
ProcessRequestEndTrace();
-
-
-
catch (ThreadAbortException) {
-
-
-
ProcessRequestEndTrace();
-
-
-
-
if (includeStagesAfterAsyncPoint) {
-
-
-
-
-
-
15、进入ProcessRequestMain,开始Page页面主体处理过程
-
private void ProcessRequestMain(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) {
-
-
HttpContext con = Context;
-
-
string exportedWebPartID =
null;
-
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"Begin PreInit");
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_ENTER, _context.WorkerRequest);
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_LEAVE, _context.WorkerRequest);
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"End PreInit");
-
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"Begin Init");
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_ENTER, _context.WorkerRequest);
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_LEAVE, _context.WorkerRequest);
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"End Init");
-
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"Begin InitComplete");
-
OnInitComplete(EventArgs.Empty);
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"End InitComplete");
-
-
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"Begin LoadState");
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_ENTER, _context.WorkerRequest);
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_LEAVE, _context.WorkerRequest);
-
if (con.TraceIsEnabled) {
-
Trace.Write(
"aspx.page",
"End LoadState");
-
Trace.Write(
"aspx.page",
"Begin ProcessPostData");
-
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_ENTER, _context.WorkerRequest);
-
ProcessPostData(_requestValueCollection,
true
);
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_LEAVE, _context.WorkerRequest);
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"End ProcessPostData");
-
-
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"Begin PreLoad");
-
OnPreLoad(EventArgs.Empty);
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"End PreLoad");
-
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"Begin Load");
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_ENTER, _context.WorkerRequest);
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_LEAVE, _context.WorkerRequest);
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"End Load");
-
-
-
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"Begin ProcessPostData Second Try");
-
"color:#FF0000;"> ProcessPostData(_leftoverPostData,
false
);
-
if (con.TraceIsEnabled) {
-
Trace.Write(
"aspx.page",
"End ProcessPostData Second Try");
-
Trace.Write(
"aspx.page",
"Begin Raise ChangedEvents");
-
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_ENTER, _context.WorkerRequest);
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_LEAVE, _context.WorkerRequest);
-
if (con.TraceIsEnabled) {
-
Trace.Write(
"aspx.page",
"End Raise ChangedEvents");
-
Trace.Write(
"aspx.page",
"Begin Raise PostBackEvent");
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_ENTER, _context.WorkerRequest);
-
RaisePostBackEvent(_requestValueCollection);
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_LEAVE, _context.WorkerRequest);
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"End Raise PostBackEvent");
-
-
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"Begin LoadComplete");
-
OnLoadComplete(EventArgs.Empty);
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"End LoadComplete");
-
-
if (IsPostBack && IsCallback) {
-
PrepareCallback(callbackControlId);
-
-
else
if (!IsCrossPagePostBack) {
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"Begin PreRender");
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_ENTER, _context.WorkerRequest);
-
PreRenderRecursiveInternal();
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_LEAVE, _context.WorkerRequest);
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"End PreRender");
-
-
-
-
-
-
if (_legacyAsyncInfo ==
null || _legacyAsyncInfo.CallerIsBlocking) {
-
-
-
ExecuteRegisteredAsyncTasks();
-
-
-
-
ValidateRawUrlIfRequired();
-
-
if (includeStagesAfterAsyncPoint) {
-
-
-
-
-
-
if (IsCrossPagePostBack) {
-
-
-
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"Begin PreRenderComplete");
-
PerformPreRenderComplete();
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"End PreRenderComplete");
-
-
if (con.TraceIsEnabled) {
-
BuildPageProfileTree(EnableViewState);
-
Trace.Write(
"aspx.page",
"Begin SaveState");
-
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_ENTER, _context.WorkerRequest);
-
-
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_LEAVE, _context.WorkerRequest);
-
if (con.TraceIsEnabled) {
-
Trace.Write(
"aspx.page",
"End SaveState");
-
Trace.Write(
"aspx.page",
"Begin SaveStateComplete");
-
-
OnSaveStateComplete(EventArgs.Empty);
-
if (con.TraceIsEnabled) {
-
Trace.Write(
"aspx.page",
"End SaveStateComplete");
-
Trace.Write(
"aspx.page",
"Begin Render");
-
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_ENTER, _context.WorkerRequest);
-
-
if (exportedWebPartID !=
null) {
-
ExportWebPart(exportedWebPartID);
-
-
-
RenderControl(CreateHtmlTextWriter(Response.Output));
-
-
-
if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_LEAVE, _context.WorkerRequest);
-
-
if (con.TraceIsEnabled) Trace.Write(
"aspx.page",
"End Render");
-
-
CheckRemainingAsyncTasks(
false);
-
-
-
至此,整个IIS处理Asp.Net完整请求就结束了,列举了各个关键处理阶段的处理函数。当我们了解.net framework内部处理机制后能够使我们理解Asp.Net运行机制和本质,对开发人员来说这很重要,不能只停留在会用会写的阶段,而且要知道其内部原理。这样不仅可以帮助我们编写更高效的代码,同时可以学习并借鉴微软的面向对象的编程思想,受益颇多!