Asp.Net 详解IIS内部运行原理

Asp.Net 详解IIS内部运行原理

本章节主要讲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函数

  1. internal static int ProcessRequestNotification(
  2. IntPtr rootedObjectsPointer,
  3. IntPtr nativeRequestContext,
  4. IntPtr moduleData,
  5. int flags)
  6. {
  7. try {
  8. //调用ProcessRequestNotificationHelper
  9.   return ProcessRequestNotificationHelper(rootedObjectsPointer, nativeRequestContext, moduleData, flags);
  10. }
  11. catch(Exception e) {
  12. ApplicationManager.RecordFatalException(e);
  13. throw;
  14. }
  15. }

2、在ProcessRequestNotificationHelper函数中调用运行时HttpRuntime中的ProcessRequestNotification函数

  1. internal static int ProcessRequestNotificationHelper(
  2. IntPtr rootedObjectsPointer,
  3. IntPtr nativeRequestContext,
  4. IntPtr moduleData,
  5. int flags)
  6. {
  7. IIS7WorkerRequest wr = null;
  8. HttpContext context = null;
  9. RequestNotificationStatus status = RequestNotificationStatus.Continue;
  10. RootedObjects root;
  11. bool workerRequestWasJustCreated = false;
  12. try {
  13. status = HttpRuntime.ProcessRequestNotification(wr, context); //调用ProcessRequestNotification函数
  14. }
  15. finally {
  16. }
  17. return ( int)status;
  18. }

3、在ProcessRequestNotification中调用ProcessRequestNotificationPrivate

4、处理请求通知ProcessRequestNotificationPrivate函数中调用了HttpApplicatioinFactory的GetApplicationInstance()函数来获取HttpApplication实例对象,并调用BeginProcessRequestNotification处理请求

  1. private RequestNotificationStatus ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) {
  2. RequestNotificationStatus status = RequestNotificationStatus.Pending;
  3. try {
  4. IHttpHandler handler = null;
  5. if (context.NeedToInitializeApp()) {
  6. try {
  7. EnsureFirstRequestInit(context);
  8. }
  9. catch {
  10. }
  11. context.Response.InitResponseWriter();
  12. handler = HttpApplicationFactory.GetApplicationInstance(context); //获取HttpApplication实例
  13. if (handler == null)
  14. throw new HttpException(SR.GetString(SR.Unable_create_app_object));
  15. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, context.WorkerRequest, handler.GetType().FullName, "Start");
  16. HttpApplication app = handler as HttpApplication;
  17. if (app != null) {
  18. // associate the context with an application instance
  19. app.AssignContext(context);
  20. }
  21. }
  22. wr.SynchronizeVariables(context);
  23. if (context.ApplicationInstance != null) {
  24. // 处理请求
  25. IAsyncResult ar = context.ApplicationInstance.BeginProcessRequestNotification(context, _requestNotificationCompletionCallback);
  26. if (ar.CompletedSynchronously) {
  27. status = RequestNotificationStatus.Continue;
  28. }
  29. }
  30. else if (handler != null) {
  31. // HttpDebugHandler is processed here
  32. handler.ProcessRequest(context);
  33. status = RequestNotificationStatus.FinishRequest;
  34. }
  35. else {
  36. status = RequestNotificationStatus.Continue;
  37. }
  38. }
  39. catch (Exception e) {
  40. }
  41. return status;
  42. }

IIS经典模式:

在ISAPIRuntime.cs类中请求 ProcessRequest函数并调用HttpRuntime.ProcessReques方法

1、HttpRuntime处理请求入口函数

  1. public static void ProcessRequest(HttpWorkerRequest wr) {
  2. if (wr == null)
  3. throw new ArgumentNullException( "wr");
  4. if (HttpRuntime.UseIntegratedPipeline) {
  5. throw new PlatformNotSupportedException(SR.GetString(SR.Method_Not_Supported_By_Iis_Integrated_Mode, "HttpRuntime.ProcessRequest"));
  6. }
  7. ProcessRequestNoDemand(wr); //调用ProcessRequestNoDemand函数
  8. }


2、ProcessRequestNoDemand函数,调用ProcessRequestNow函数

  1. internal static void ProcessRequestNoDemand(HttpWorkerRequest wr) {
  2. RequestQueue rq = _theRuntime._requestQueue;
  3. wr.UpdateInitialCounters();
  4. if (rq != null) // could be null before first request
  5. wr = rq.GetRequestToExecute(wr);
  6. if (wr != null) {
  7. CalculateWaitTimeAndUpdatePerfCounter(wr);
  8. wr.ResetStartTime();
  9. ProcessRequestNow(wr);调用ProcessRequestNow
  10. }
  11. }

3、ProcessRequestNow函数,调用ProcessRequestInternal

  1. internal static void ProcessRequestNow(HttpWorkerRequest wr)
  2. {
  3. _theRuntime.ProcessRequestInternal(wr); //调用ProcessRequestInternal
  4. }

4、ProcessRequestInternal函数,调用HttpApplicatioinFactory的GetApplicationInstance()函数来获取HttpApplication实例对象

  1. private void ProcessRequestInternal(HttpWorkerRequest wr) {
  2. // Count active requests
  3. Interlocked.Increment( ref _activeRequestCount);
  4. // Get application instance
  5. IHttpHandler app = HttpApplicationFactory.GetApplicationInstance(context); //获取HttpApplication对象
  6. if (app == null)
  7. throw new HttpException(SR.GetString(SR.Unable_create_app_object));
  8. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, context.WorkerRequest, app.GetType().FullName, "Start");
  9. if (app is IHttpAsyncHandler) {
  10. // asynchronous handler
  11. IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler)app;
  12. context.AsyncAppHandler = asyncHandler;
  13. //来到了真正处理请求步骤
  14. asyncHandler.BeginProcessRequest(context, _handlerCompletionCallback, context);
  15. }
  16. else {
  17. // synchronous handler
  18. app.ProcessRequest(context);
  19. FinishRequest(context.WorkerRequest, context, null);
  20. }
  21. }
  22. catch (Exception e) {
  23. context.Response.InitResponseWriter();
  24. FinishRequest(wr, context, e);
  25. }
  26. }

说明:以上4个步骤集成模式和经典模式分不同的函数执行,从下面开始两种模式调用相同的函数,只是在函数中针对不同的模式进行不同的处理

5、进入HttpApplicationFactory,调用GetNormalApplicationInstance函数

  1. internal static IHttpHandler GetApplicationInstance(HttpContext context) {
  2. if (_customApplication != null)
  3. return _customApplication;
  4.   if (context.Request.IsDebuggingRequest)
  5. return new HttpDebugHandler();
  6. _theApplicationFactory.EnsureInited();
  7. _theApplicationFactory.EnsureAppStartCalled(context);
  8. return _theApplicationFactory.GetNormalApplicationInstance(context); //返回HttpApplication实例
  9. }

6、进入GetNormalApplicationInstance函数,调用InitInternal初始化HttpApplication内部对象

  1. private HttpApplication GetNormalApplicationInstance(HttpContext context) {
  2. HttpApplication app = null;
  3. lock (_freeList) {
  4. if (_numFreeAppInstances > 0) {
  5. app = (HttpApplication)_freeList.Pop();
  6. _numFreeAppInstances--;
  7. if (_numFreeAppInstances < _minFreeAppInstances) {
  8. _minFreeAppInstances = _numFreeAppInstances;
  9. }
  10. }
  11. }
  12. if (app == null) {
  13. // If ran out of instances, create a new one
  14. app = (HttpApplication)HttpRuntime.CreateNonPublicInstance(_theApplicationType);
  15. using ( new ApplicationImpersonationContext()) {
  16. app.InitInternal(context, _state, _eventHandlerMethods); //初始化Application内部对象
  17. }
  18. }
  19. if (AppSettings.UseTaskFriendlySynchronizationContext) {
  20. // When this HttpApplication instance is no longer in use, recycle it.
  21. app.ApplicationInstanceConsumersCounter = new CountdownTask( 1); // representing required call to HttpApplication.ReleaseAppInstance
  22. app.ApplicationInstanceConsumersCounter.Task.ContinueWith((_, o) => RecycleApplicationInstance((HttpApplication)o), app, TaskContinuationOptions.ExecuteSynchronously);
  23. }
  24. return app;
  25. }

7、进入HttpApplication,调用InitInternal函数来初始化Application内部对象。初始化HttpModule和建立处理执行步骤


  1. internal void InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers) {
  2. Debug.Assert(context != null, "context != null");
  3. // Remember state
  4. _state = state;
  5. PerfCounters.IncrementCounter(AppPerfCounter.PIPELINES);
  6. try {
  7. try {
  8. // Remember context for config lookups
  9. _initContext = context;
  10. _initContext.ApplicationInstance = this;
  11. // Set config path to be application path for the application initialization
  12. context.ConfigurationPath = context.Request.ApplicationPathObject;
  13. // keep HttpContext.Current working while running user code
  14. using ( new DisposableHttpContextWrapper(context)) {
  15. // Build module list from config 根据IIS集成模式和经典模式初始化HttpMudule
  16. if (HttpRuntime.UseIntegratedPipeline) { //集成模式
  17. Debug.Assert(_moduleConfigInfo != null, "_moduleConfigInfo != null");
  18. Debug.Assert(_moduleConfigInfo.Count >= 0, "_moduleConfigInfo.Count >= 0");
  19. try {
  20. context.HideRequestResponse = true;
  21. _hideRequestResponse = true;
  22. InitIntegratedModules(); //集成模式初始化HttpModule
  23. }
  24. finally {
  25. context.HideRequestResponse = false;
  26. _hideRequestResponse = false;
  27. }
  28. }
  29. else { //经典模式
  30. InitModules(); //经典模式初始化HttpModule
  31. // this is used exclusively for integrated mode
  32. Debug.Assert( null == _moduleContainers, "null == _moduleContainers");
  33. }
  34. // Hookup event handlers via reflection
  35. if (handlers != null)
  36. HookupEventHandlersForApplicationAndModules(handlers);
  37. // Initialization of the derived class
  38. _context = context;
  39. if (HttpRuntime.UseIntegratedPipeline && _context != null) {
  40. _context.HideRequestResponse = true;
  41. }
  42. _hideRequestResponse = true;
  43. try {
  44. Init();
  45. }
  46. catch (Exception e) {
  47. RecordError(e);
  48. }
  49. }
  50. if (HttpRuntime.UseIntegratedPipeline && _context != null) {
  51. _context.HideRequestResponse = false;
  52. }
  53. _hideRequestResponse = false;
  54. _context = null;
  55. _resumeStepsWaitCallback= new WaitCallback( this.ResumeStepsWaitCallback);
  56. // Construct the execution steps array
  57. <span style= "color:#FF0000;"><strong> if (HttpRuntime.UseIntegratedPipeline) {</strong></span> //集成模式 调用PipelineStepManager
  58. <span style= "color:#FF0000;"><strong>_stepManager = new PipelineStepManager( this);</strong></span>
  59. }
  60. else {
  61. <span style= "color:#FF0000;"><strong> _stepManager = new ApplicationStepManager( this);</strong></span> //经典模式 调用 ApplicationStepManager
  62. }
  63. <span style= "color:#FF0000;"><strong>_stepManager.BuildSteps(_resumeStepsWaitCallback);</strong></span> //建立执行处理步骤
  64. }
  65. finally {
  66. _initInternalCompleted = true;
  67. // Reset config path
  68. context.ConfigurationPath = null;
  69. // don't hold on to the context
  70. _initContext.ApplicationInstance = null;
  71. _initContext = null;
  72. }
  73. }
  74. catch { // Protect against exception filters
  75. throw;
  76. }
  77. }

8、初始化HttpModule,包含系统默认的HttpModule和自定义的HttpModule

1)、系统默认的HttpModule:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config


2)、自定义的HttpModule在网站的Web.config中配置

经典模式:

  1. <system.web>
  2. <httpModules>
  3. < add name= "mymodule" type= "WebApplication2.myModule"/>
  4. </httpModules>
  5. </system.web>
集成模式:
  1. <system.webServer>
  2. <modules>
  3. < add name= "mymodule" type= "WebApplication2.myModule"/>
  4. </modules>
  5. </system.webServer>

3)、经典模式初始化:

  1. private void InitModules() {
  2. //读取配置文件中HttpModules节点
  3.  HttpModulesSection pconfig = RuntimeConfig.GetAppConfig().HttpModules;
  4. //获取HttpModule集合
  5. HttpModuleCollection moduleCollection = pconfig.CreateModules();
  6. HttpModuleCollection dynamicModules = CreateDynamicModules();
  7. moduleCollection.AppendCollection(dynamicModules);
  8. _moduleCollection = moduleCollection; // don't assign until all ops have succeeded
  9. InitModulesCommon(); //调用InitModulesCommon
  10. }

4)、初始化各个HttpModule对象.

  1. private void InitModulesCommon() {
  2. int n = _moduleCollection.Count;
  3. for ( int i = 0; i < n; i++) {
  4. // remember the module being inited for event subscriptions
  5. // we'll later use this for routing
  6. _currentModuleCollectionKey = _moduleCollection.GetKey(i);
  7. _moduleCollection[i].Init( this); //初始化每个HttpModule
  8. }
  9. _currentModuleCollectionKey = null;
  10. InitAppLevelCulture();
  11. }

5)、初始化HttpModule,在Init函数中注册HttpApplication对象的事件,这里举了两个例子

OutputCacheModule.cs  web.config中默认第一个HttpModule

  1. void IHttpModule.Init(HttpApplication app) {
  2. OutputCacheSection cacheConfig = RuntimeConfig.GetAppConfig().OutputCache;
  3. if (cacheConfig.EnableOutputCache) {
  4. app.ResolveRequestCache += new EventHandler( this.OnEnter);
  5. app.UpdateRequestCache += new EventHandler( this.OnLeave);
  6. }
  7. }

SessionStateModule.cs web.config中默认第二个HttpModule,实现Session的系统HttpModule

  1. public void Init(HttpApplication app) {
  2. bool initModuleCalled = false;
  3. SessionStateSection config = RuntimeConfig.GetAppConfig().SessionState;
  4. if (!s_oneTimeInit) {
  5. s_lock.AcquireWriterLock();
  6. try {
  7. if (!s_oneTimeInit) {
  8. InitModuleFromConfig(app, config);
  9. initModuleCalled = true;
  10. if (!CheckTrustLevel(config))
  11. s_trustLevelInsufficient = true;
  12. s_timeout = ( int)config.Timeout.TotalMinutes;
  13. s_useHostingIdentity = config.UseHostingIdentity;
  14. // See if we can try InProc optimization. See inline doc of s_allowInProcOptimization
  15. // for details.
  16. if (config.Mode == SessionStateMode.InProc &&
  17. _usingAspnetSessionIdManager) {
  18. s_allowInProcOptimization = true;
  19. }
  20. if (config.Mode != SessionStateMode.Custom &&
  21. config.Mode != SessionStateMode.Off &&
  22. !config.RegenerateExpiredSessionId) {
  23. s_allowDelayedStateStoreItemCreation = true;
  24. }
  25. s_configExecutionTimeout = RuntimeConfig.GetConfig().HttpRuntime.ExecutionTimeout;
  26. s_configRegenerateExpiredSessionId = config.RegenerateExpiredSessionId;
  27. s_configCookieless = config.Cookieless;
  28. s_configMode = config.Mode;
  29. // The last thing to set in this if-block.
  30. s_oneTimeInit = true;
  31. Debug.Trace( "SessionStateModuleInit",
  32. "Configuration: _mode=" + config.Mode +
  33. ";Timeout=" + config.Timeout +
  34. ";CookieMode=" + config.Cookieless +
  35. ";SqlConnectionString=" + config.SqlConnectionString +
  36. ";StateConnectionString=" + config.StateConnectionString +
  37. ";s_allowInProcOptimization=" + s_allowInProcOptimization +
  38. ";s_allowDelayedStateStoreItemCreation=" + s_allowDelayedStateStoreItemCreation);
  39. }
  40. }
  41. finally {
  42. s_lock.ReleaseWriterLock();
  43. }
  44. }
  45. if (!initModuleCalled) {
  46. InitModuleFromConfig(app, config);
  47. }
  48. if (s_trustLevelInsufficient) {
  49. throw new HttpException(SR.GetString(SR.Session_state_need_higher_trust));
  50. }
  51. }

集成模式初始化HttpModule代码我就不贴出来了,大家可以在源码中查看

9、建立处理步骤BuildSteps,将所有要执行的步骤载入到一个IExecutionStep[]集合中,待后面ResumeSteps调用

经典模式:调用ApplicationStepManager类中的BuildSteps函数

  1. internal class ApplicationStepManager : StepManager {
  2. private IExecutionStep[] _execSteps;
  3. private WaitCallback _resumeStepsWaitCallback;
  4. private int _currentStepIndex;
  5. private int _numStepCalls;
  6. private int _numSyncStepCalls;
  7. private int _endRequestStepIndex;
  8. internal ApplicationStepManager(HttpApplication app): base(app) {
  9. }
  10. internal override void BuildSteps(WaitCallback stepCallback )
  11. {
  12. ArrayList steps = new ArrayList();
  13. HttpApplication app = _application;
  14. bool urlMappingsEnabled = false;
  15. UrlMappingsSection urlMappings = RuntimeConfig.GetConfig().UrlMappings;
  16. urlMappingsEnabled = urlMappings.IsEnabled && ( urlMappings.UrlMappings.Count > 0 );
  17. steps.Add( new ValidateRequestExecutionStep(app));
  18. steps.Add( new ValidatePathExecutionStep(app));
  19. if (urlMappingsEnabled)
  20. steps.Add( new UrlMappingsExecutionStep(app)); // url mappings
  21. app.CreateEventExecutionSteps(HttpApplication.EventBeginRequest, steps);
  22. app.CreateEventExecutionSteps(HttpApplication.EventAuthenticateRequest, steps);
  23. app.CreateEventExecutionSteps(HttpApplication.EventDefaultAuthentication, steps);
  24. app.CreateEventExecutionSteps(HttpApplication.EventPostAuthenticateRequest, steps);
  25. app.CreateEventExecutionSteps(HttpApplication.EventAuthorizeRequest, steps);
  26. app.CreateEventExecutionSteps(HttpApplication.EventPostAuthorizeRequest, steps);
  27. app.CreateEventExecutionSteps(HttpApplication.EventResolveRequestCache, steps);
  28. app.CreateEventExecutionSteps(HttpApplication.EventPostResolveRequestCache, steps);
  29. steps.Add( new MapHandlerExecutionStep(app)); //分配一个Handler
  30. app.CreateEventExecutionSteps(HttpApplication.EventPostMapRequestHandler, steps);
  31. app.CreateEventExecutionSteps(HttpApplication.EventAcquireRequestState, steps);
  32. app.CreateEventExecutionSteps(HttpApplication.EventPostAcquireRequestState, steps);
  33. app.CreateEventExecutionSteps(HttpApplication.EventPreRequestHandlerExecute, steps);
  34. steps.Add(app.CreateImplicitAsyncPreloadExecutionStep()); // implict async preload step
  35. steps.Add( new CallHandlerExecutionStep(app)); // 执行HttpHandler
  36. app.CreateEventExecutionSteps(HttpApplication.EventPostRequestHandlerExecute, steps);
  37. app.CreateEventExecutionSteps(HttpApplication.EventReleaseRequestState, steps);
  38. app.CreateEventExecutionSteps(HttpApplication.EventPostReleaseRequestState, steps);
  39. steps.Add( new CallFilterExecutionStep(app)); // filtering
  40. app.CreateEventExecutionSteps(HttpApplication.EventUpdateRequestCache, steps);
  41. app.CreateEventExecutionSteps(HttpApplication.EventPostUpdateRequestCache, steps);
  42. _endRequestStepIndex = steps.Count;
  43. app.CreateEventExecutionSteps(HttpApplication.EventEndRequest, steps);
  44. steps.Add( new NoopExecutionStep()); // the last is always there
  45. _execSteps = new IExecutionStep[steps.Count];
  46. steps.CopyTo(_execSteps);
  47. // callback for async completion when reposting to threadpool thread
  48. _resumeStepsWaitCallback = stepCallback;
  49. }

集成模式:调用PipelineStepManager类中的 BuildSteps函数

  1. internal override void BuildSteps(WaitCallback stepCallback) {
  2. Debug.Trace( "PipelineRuntime", "BuildSteps");
  3. //ArrayList steps = new ArrayList();
  4. HttpApplication app = _application;
  5. // add special steps that don't currently
  6. // correspond to a configured handler
  7. IExecutionStep materializeStep = new MaterializeHandlerExecutionStep(app);
  8. // implicit map step
  9. app.AddEventMapping(
  10. HttpApplication.IMPLICIT_HANDLER,
  11. RequestNotification.MapRequestHandler,
  12. false, materializeStep);
  13. // implicit async preload step
  14. app.AddEventMapping(
  15. HttpApplication.IMPLICIT_HANDLER,
  16. RequestNotification.ExecuteRequestHandler,
  17. false, app.CreateImplicitAsyncPreloadExecutionStep());
  18. // implicit handler routing step
  19. IExecutionStep handlerStep = new CallHandlerExecutionStep(app);
  20. app.AddEventMapping(
  21. HttpApplication.IMPLICIT_HANDLER,
  22. RequestNotification.ExecuteRequestHandler,
  23. false, handlerStep);
  24. // implicit handler WebSockets step
  25. IExecutionStep webSocketsStep = new TransitionToWebSocketsExecutionStep(app);
  26. app.AddEventMapping(
  27. HttpApplication.IMPLICIT_HANDLER,
  28. RequestNotification.EndRequest,
  29. true /* isPostNotification */, webSocketsStep);
  30. // add implicit request filtering step
  31. IExecutionStep filterStep = new CallFilterExecutionStep(app);
  32. // normally, this executes during UpdateRequestCache as a high priority module
  33. app.AddEventMapping(
  34. HttpApplication.IMPLICIT_FILTER_MODULE,
  35. RequestNotification.UpdateRequestCache,
  36. false, filterStep);
  37. // for error conditions, this executes during LogRequest as a high priority module
  38. app.AddEventMapping(
  39. HttpApplication.IMPLICIT_FILTER_MODULE,
  40. RequestNotification.LogRequest,
  41. false, filterStep);
  42. _resumeStepsWaitCallback = stepCallback;
  43. }


10、真正处理请求阶段, 调用ResumeSteps函数来执行处理步骤

经典模式:

返回到ProcessRequestInternal函数中执行BeginProcessRequest函数

  1. IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) {
  2. HttpAsyncResult result;
  3. // Setup the asynchronous stuff and application variables
  4. _context = context;
  5. _context.ApplicationInstance = this;
  6. _stepManager.InitRequest();
  7. // Make sure the context stays rooted (including all async operations)
  8. _context.Root();
  9. // Create the async result
  10. result = new HttpAsyncResult(cb, extraData);
  11. // Remember the async result for use in async completions
  12. AsyncResult = result;
  13. if (_context.TraceIsEnabled)
  14. HttpRuntime.Profile.StartRequest(_context);
  15. // Start the application
  16. ResumeSteps( null); //执行处理步骤
  17. // Return the async result
  18. return result;
  19. }

ResumeSteps函数,调用ExecuteStepl函数来执行处理步骤

  1. internal override void ResumeSteps(Exception error) {
  2. bool appCompleted = false;
  3. bool stepCompletedSynchronously = true;
  4. HttpApplication app = _application;
  5. CountdownTask appInstanceConsumersCounter = app.ApplicationInstanceConsumersCounter;
  6. HttpContext context = app.Context;
  7. ThreadContext threadContext = null;
  8. AspNetSynchronizationContextBase syncContext = context.SyncContext;
  9. Debug.Trace( "Async", "HttpApplication.ResumeSteps");
  10. try {
  11. if (appInstanceConsumersCounter != null) {
  12. appInstanceConsumersCounter.MarkOperationPending(); // ResumeSteps call started
  13. }
  14. using (syncContext.AcquireThreadLock()) {
  15. //执行步骤
  16. error = app.ExecuteStep(_execSteps[_currentStepIndex], ref stepCompletedSynchronously);
  17. }
  18. }

集成模式:

返回到ProcessRequestNotificationPrivate函数,执行BeginProcessRequestNotification

BeginProcessRequestNotification函数,调用ResumeSteps执行处理步骤

  1. internal IAsyncResult BeginProcessRequestNotification(HttpContext context, AsyncCallback cb)
  2. {
  3. if ( this._context == null)
  4. {
  5. this.AssignContext(context);
  6. }
  7. context.CurrentModuleEventIndex = -1;
  8. HttpAsyncResult result = new HttpAsyncResult(cb, context);
  9. context.NotificationContext.AsyncResult = result;
  10. this.ResumeSteps( null); //开始执行处理步骤
  11. return result;
  12. }


 ResumeSteps

  1. // PipelineStepManager::ResumeSteps
  2. // called from IIS7 (on IIS thread) via BeginProcessRequestNotification
  3. // or from an async completion (on CLR thread) via HttpApplication::ResumeStepsFromThreadPoolThread
  4. // This attribute prevents undesirable 'just-my-code' debugging behavior (VSWhidbey 404406/VSWhidbey 609188)
  5. [ System.Diagnostics.DebuggerStepperBoundaryAttribute]
  6. internal override void ResumeSteps(Exception error) {
  7. HttpContext context = _application.Context;
  8. IIS7WorkerRequest wr = context.WorkerRequest as IIS7WorkerRequest;
  9. AspNetSynchronizationContextBase syncContext = context.SyncContext;
  10. RequestNotificationStatus status = RequestNotificationStatus.Continue;
  11. ThreadContext threadContext = null;
  12. bool needToDisassociateThreadContext = false;
  13. bool isSynchronousCompletion = false;
  14. bool needToComplete = false;
  15. bool stepCompletedSynchronously = false;
  16. int currentModuleLastEventIndex = _application.CurrentModuleContainer.GetEventCount(context.CurrentNotification, context.IsPostNotification) - 1;
  17. CountdownTask appInstanceConsumersCounter = _application.ApplicationInstanceConsumersCounter;
  18. using (context.RootedObjects.WithinTraceBlock()) {
  19. error = _application.ExecuteStep(step, ref stepCompletedSynchronously); //执行处理步骤
  20. }
  21. }

11、ExecuteStep执行BuildSteps中的各个步骤

  1. internal Exception ExecuteStep(IExecutionStep step, ref bool completedSynchronously) {
  2. Exception error = null;
  3. try {
  4. try {
  5. if (step.IsCancellable) {
  6. _context.BeginCancellablePeriod(); // request can be cancelled from this point
  7. try {
  8. step.Execute();
  9. }
  10. finally {
  11. _context.EndCancellablePeriod(); // request can be cancelled until this point
  12. }
  13. _context.WaitForExceptionIfCancelled(); // wait outside of finally
  14. }
  15. else {
  16. step.Execute();</strong></span>
  17. }
  18. if (!step.CompletedSynchronously) {
  19. completedSynchronously = false;
  20. return null;
  21. }
  22. }
  23. }


这里贴出了两个很重要的步骤:获取HttpHandler(MaterializeHandlerExecutionStep)和执行HttpHandler(CallHandlerExecutionStep)

经典模式:

MapHandlerExecutionStep

  1. // execution step -- map HTTP handler (used to be a separate module)
  2. internal class < span style= "color:#FF0000;"><strong>MapHandlerExecutionStep </strong></span>: IExecutionStep {
  3. private HttpApplication _application;
  4. internal MapHandlerExecutionStep(HttpApplication app) {
  5. _application = app;
  6. }
  7. void IExecutionStep.Execute() {
  8. HttpContext context = _application.Context;
  9. HttpRequest request = context.Request;
  10. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_ENTER, context.WorkerRequest);
  11. //获取HttpHandler
  12. context.Handler = _application.MapHttpHandler(
  13. context,
  14. request.RequestType,
  15. request.FilePathObject,
  16. request.PhysicalPathInternal,
  17. false /*useAppConfig*/);</strong></span>
  18. Debug.Assert(context.ConfigurationPath == context.Request.FilePathObject, "context.ConfigurationPath (" +
  19. context.ConfigurationPath + ") != context.Request.FilePath (" + context.Request.FilePath + ")");
  20. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_LEAVE, context.WorkerRequest);
  21. }
  22. bool IExecutionStep.CompletedSynchronously {
  23. get { return true;}
  24. }
  25. bool IExecutionStep.IsCancellable {
  26. get { return false; }
  27. }
  28. }

MapHttpHandler获取处理请求的HttpHandler

  1. internal IHttpHandler MapHttpHandler(HttpContext context, string requestType, VirtualPath path, string pathTranslated, bool useAppConfig)
  2. {
  3. IHttpHandler handler = (context.ServerExecuteDepth == 0) ? context.RemapHandlerInstance : null;
  4. using ( new ApplicationImpersonationContext())
  5. {
  6. if (handler != null)
  7. {
  8. return handler;
  9. }
  10. HttpHandlerAction mapping = this.GetHandlerMapping(context, requestType, path, useAppConfig);
  11. if (mapping == null)
  12. {
  13. PerfCounters.IncrementCounter(AppPerfCounter.REQUESTS_NOT_FOUND);
  14. PerfCounters.IncrementCounter(AppPerfCounter.REQUESTS_FAILED);
  15. throw new HttpException(SR.GetString( "Http_handler_not_found_for_request_type", new object[] { requestType }));
  16. }
  17. IHttpHandlerFactory factory = this.GetFactory(mapping); //获取HttpHandlerFactory对象,如果网站Web.config中自定义了HttpHandlerFactory对象,则会覆盖系统默认的
  18. try
  19. {
  20. IHttpHandlerFactory2 factory2 = factory as IHttpHandlerFactory2;
  21. if (factory2 != null)
  22. {
  23. handler = factory2.GetHandler(context, requestType, path, pathTranslated); //获取HttpHandler对象,如果为自定义的HttpHandlerFactory,则返回自定义HttpHandlerFactory中设置的HttpHandler
  24. }
  25. else
  26. {
  27. handler = factory.GetHandler(context, requestType, path.VirtualPathString, pathTranslated);
  28. }
  29. }
  30. catch (FileNotFoundException exception)
  31. {
  32. if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
  33. {
  34. throw new HttpException( 0x194, null, exception);
  35. }
  36. throw new HttpException( 0x194, null);
  37. }
  38. catch (DirectoryNotFoundException exception2)
  39. {
  40. if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
  41. {
  42. throw new HttpException( 0x194, null, exception2);
  43. }
  44. throw new HttpException( 0x194, null);
  45. }
  46. catch (PathTooLongException exception3)
  47. {
  48. if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
  49. {
  50. throw new HttpException( 0x19e, null, exception3);
  51. }
  52. throw new HttpException( 0x19e, null);
  53. }
  54. if ( this._handlerRecycleList == null)
  55. {
  56. this._handlerRecycleList = new ArrayList();
  57. }
  58. this._handlerRecycleList.Add( new HandlerWithFactory(handler, factory));
  59. }
  60. return handler;
  61. }


集成模式:

MaterializeHandlerExecutionStep步骤

  1. internal class MaterializeHandlerExecutionStep: IExecutionStep {
  2. private HttpApplication _application;
  3. internal MaterializeHandlerExecutionStep(HttpApplication app) {
  4. _application = app;
  5. }
  6. void IExecutionStep.Execute() {
  7. HttpContext context = _application.Context;
  8. HttpRequest request = context.Request;
  9. IHttpHandler handler = null;
  10. string configType = null;
  11. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_ENTER, context.WorkerRequest);
  12. IIS7WorkerRequest wr = context.WorkerRequest as IIS7WorkerRequest;
  13. // Get handler
  14. if (context.RemapHandlerInstance != null){
  15. //RemapHandler overrides all
  16. wr.SetScriptMapForRemapHandler();
  17. context.Handler = context.RemapHandlerInstance;
  18. }
  19. else if (request.RewrittenUrl != null) {
  20. // RewritePath, we need to re-map the handler
  21. bool handlerExists;
  22. configType = wr.ReMapHandlerAndGetHandlerTypeString(context, request.Path, out handlerExists);
  23. if (!handlerExists) {
  24. // WOS 1973590: When RewritePath is used with missing handler in Integrated Mode,an empty response 200 is returned instead of 404
  25. throw new HttpException( 404, SR.GetString(SR.Http_handler_not_found_for_request_type, request.RequestType));
  26. }
  27. }
  28. else {
  29. configType = wr.GetManagedHandlerType();
  30. }
  31. if (!String.IsNullOrEmpty(configType)) {
  32. IHttpHandlerFactory factory = _application.GetFactory(configType); //获取HttpHandlerFactory对象
  33. string pathTranslated = request.PhysicalPathInternal;
  34. try {
  35. handler = factory.GetHandler(context, request.RequestType, request.FilePath, pathTranslated); //获取HttpHandler对象
  36. }
  37. catch (FileNotFoundException e) {
  38. if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
  39. throw new HttpException( 404, null, e);
  40. else
  41. throw new HttpException( 404, null);
  42. }
  43. catch (DirectoryNotFoundException e) {
  44. if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
  45. throw new HttpException( 404, null, e);
  46. else
  47. throw new HttpException( 404, null);
  48. }
  49. catch (PathTooLongException e) {
  50. if (HttpRuntime.HasPathDiscoveryPermission(pathTranslated))
  51. throw new HttpException( 414, null, e);
  52. else
  53. throw new HttpException( 414, null);
  54. }
  55. context.Handler = handler;
  56. // Remember for recycling
  57. if (_application._handlerRecycleList == null)
  58. _application._handlerRecycleList = new ArrayList();
  59. _application._handlerRecycleList.Add( new HandlerWithFactory(handler, factory));
  60. }
  61. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Infrastructure)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_MAPHANDLER_LEAVE, context.WorkerRequest);
  62. }
  63. bool IExecutionStep.CompletedSynchronously {
  64. get { return true;}
  65. }
  66. bool IExecutionStep.IsCancellable {
  67. get { return false; }
  68. }
  69. }

12、执行callhandlerexecutestep步骤,调用handler.ProcessRequest(context),如果HttpHandler类型为Page,则开始Page页生命周期

  1. internal class CallHandlerExecutionStep: IExecutionStep {
  2. private HttpApplication _application;
  3. private AsyncCallback _completionCallback;
  4. private IHttpAsyncHandler _handler; // per call
  5. private AsyncStepCompletionInfo _asyncStepCompletionInfo; // per call
  6. private bool _sync; // per call
  7. internal CallHandlerExecutionStep(HttpApplication app) {
  8. _application = app;
  9. _completionCallback = new AsyncCallback( this.OnAsyncHandlerCompletion);
  10. }
  11. voidIExecutionStep.Execute()
  12. {
  13. HttpContext context = _application.Context;
  14. IHttpHandler handler = context.Handler;
  15. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Information, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_ENTER, context.WorkerRequest);
  16. if (handler != null && HttpRuntime.UseIntegratedPipeline) {
  17. IIS7WorkerRequest wr = context.WorkerRequest as IIS7WorkerRequest;
  18. if (wr != null && wr.IsHandlerExecutionDenied()) {
  19. _sync = true;
  20. HttpException error = new HttpException( 403, SR.GetString(SR.Handler_access_denied));
  21. error.SetFormatter( new PageForbiddenErrorFormatter(context.Request.Path, SR.GetString(SR.Handler_access_denied)));
  22. throw error;
  23. }
  24. }
  25. if (handler == null) {
  26. _sync = true;
  27. }
  28. else if (handler is IHttpAsyncHandler) {
  29. // asynchronous handler
  30. IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler)handler;
  31. _sync = false;
  32. _handler = asyncHandler;
  33. var beginProcessRequestDelegate = AppVerifier.WrapBeginMethod<HttpContext>(_application, asyncHandler.BeginProcessRequest);
  34. _asyncStepCompletionInfo.Reset();
  35. context.SyncContext.AllowVoidAsyncOperations();
  36. IAsyncResult ar;
  37. try {
  38. ar = beginProcessRequestDelegate(context, _completionCallback, null);
  39. }
  40. catch {
  41. // The asynchronous step has completed, so we should disallow further
  42. // async operations until the next step.
  43. context.SyncContext.ProhibitVoidAsyncOperations();
  44. throw;
  45. }
  46. bool operationCompleted;
  47. bool mustCallEndHandler;
  48. _asyncStepCompletionInfo.RegisterBeginUnwound(ar, out operationCompleted, out mustCallEndHandler);
  49. if (operationCompleted) {
  50. _sync = true;
  51. _handler = null; // not to remember
  52. // The asynchronous step has completed, so we should disallow further
  53. // async operations until the next step.
  54. context.SyncContext.ProhibitVoidAsyncOperations();
  55. try {
  56. if (mustCallEndHandler) {
  57. asyncHandler.EndProcessRequest(ar);
  58. }
  59. _asyncStepCompletionInfo.ReportError();
  60. }
  61. finally {
  62. SuppressPostEndRequestIfNecessary(context);
  63. // In Integrated mode, generate the necessary response headers
  64. // after the ASP.NET handler runs
  65. context.Response.GenerateResponseHeadersForHandler();
  66. }
  67. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Information, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_LEAVE, context.WorkerRequest);
  68. }
  69. }
  70. else {
  71. //同步 handler
  72. _sync = true;
  73. context.SyncContext.SetSyncCaller();
  74. try {
  75. handler.ProcessRequest(context); //开始Page页面生命周期
  76. }
  77. finally {
  78. context.SyncContext.ResetSyncCaller();
  79. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Information, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_HTTPHANDLER_LEAVE, context.WorkerRequest);
  80. SuppressPostEndRequestIfNecessary(context);
  81. context.Response.GenerateResponseHeadersForHandler();
  82. }
  83. }
  84. }
  85. bool IExecutionStep.CompletedSynchronously {
  86. get { return _sync;}
  87. }
  88. bool IExecutionStep.IsCancellable {
  89. // launching of async handler should not be cancellable
  90. get { return (_application.Context.Handler is IHttpAsyncHandler) ? false : true; }
  91. }
  92. }

13、进入Page类的 ProcessRequest方法开始处理请求


  1. private void ProcessRequest() {
  2. // culture needs to be saved/restored only on synchronous pages (if at all)
  3. // save culture
  4. Thread currentThread = Thread.CurrentThread;
  5. CultureInfo prevCulture = currentThread.CurrentCulture;
  6. CultureInfo prevUICulture = currentThread.CurrentUICulture;
  7. try {
  8. ProcessRequest( true /*includeStagesBeforeAsyncPoint*/, true /*includeStagesAfterAsyncPoint*/); //调用ProcessRequest函数
  9. }
  10. finally {
  11. // restore culture
  12. RestoreCultures(currentThread, prevCulture, prevUICulture);
  13. }
  14. }

14、进入ProcessRequest,调用ProcessRequestMain函数


  1. private void ProcessRequest(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) {
  2. // Initialize the object and build the tree of controls.
  3. // This must happen *after* the intrinsics have been set.
  4. // On async pages only call Initialize once (ProcessRequest is called twice)
  5. if (includeStagesBeforeAsyncPoint) {
  6. FrameworkInitialize();
  7. this.ControlState = ControlState.FrameworkInitialized;
  8. }
  9. bool needToCallEndTrace = Context.WorkerRequest is IIS7WorkerRequest;
  10. try {
  11. try {
  12. if (IsTransacted) {
  13. ProcessRequestTransacted();
  14. }
  15. else {
  16. // No transactions
  17. ProcessRequestMain(includeStagesBeforeAsyncPoint, includeStagesAfterAsyncPoint); //调用ProcessRequestMain函数
  18. }
  19. if (includeStagesAfterAsyncPoint) {
  20. needToCallEndTrace = false;
  21. ProcessRequestEndTrace();
  22. }
  23. }
  24. catch (ThreadAbortException) {
  25. try {
  26. if (needToCallEndTrace)
  27. ProcessRequestEndTrace();
  28. } catch {}
  29. }
  30. finally {
  31. if (includeStagesAfterAsyncPoint) {
  32. ProcessRequestCleanup();
  33. }
  34. }
  35. }
  36. catch { throw; } // Prevent Exception Filter Security Issue (ASURT 122835)
  37. }

15、进入ProcessRequestMain,开始Page页面主体处理过程

  1. private void ProcessRequestMain(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) {
  2. try {
  3. HttpContext con = Context;
  4. string exportedWebPartID = null;
  5. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "Begin PreInit");
  6. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_ENTER, _context.WorkerRequest);
  7. PerformPreInit(); //Page页面生命周期的OnPerLoad阶段
  8. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_INIT_LEAVE, _context.WorkerRequest);
  9. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "End PreInit");
  10. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "Begin Init");
  11. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_ENTER, _context.WorkerRequest);
  12. InitRecursive( null);
  13. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_INIT_LEAVE, _context.WorkerRequest);
  14. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "End Init");
  15. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "Begin InitComplete");
  16. OnInitComplete(EventArgs.Empty); //Page页面生命周期的OnInitComplete阶段
  17. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "End InitComplete");
  18. if (IsPostBack) {
  19. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "Begin LoadState");
  20. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_ENTER, _context.WorkerRequest);
  21. LoadAllState(); //Page页面生命周期的LoadViewState阶段(加载视图状态)
  22. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_VIEWSTATE_LEAVE, _context.WorkerRequest);
  23. if (con.TraceIsEnabled) {
  24. Trace.Write( "aspx.page", "End LoadState");
  25. Trace.Write( "aspx.page", "Begin ProcessPostData");
  26. }
  27. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_ENTER, _context.WorkerRequest);
  28. ProcessPostData(_requestValueCollection, true /* fBeforeLoad */); //Page页面生命周期ProcessPostData阶段(处理回发数据)
  29. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_POSTDATA_LEAVE, _context.WorkerRequest);
  30. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "End ProcessPostData");
  31. }
  32. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "Begin PreLoad");
  33. OnPreLoad(EventArgs.Empty); //Page页面生命周期OnPreLoad阶段(预加载)
  34. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "End PreLoad");
  35. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "Begin Load");
  36. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_ENTER, _context.WorkerRequest);
  37. LoadRecursive(); //Page页面生命周期Load阶段(加载)
  38. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_LOAD_LEAVE, _context.WorkerRequest);
  39. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "End Load");
  40. if (IsPostBack) {
  41. // Try process the post data again (ASURT 29045)
  42. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "Begin ProcessPostData Second Try");
  43. <span style= "color:#FF0000;"><strong> ProcessPostData(_leftoverPostData, false /* !fBeforeLoad */); //Page页面生命周期 ProcessPostData阶段</strong></span>
  44. if (con.TraceIsEnabled) {
  45. Trace.Write( "aspx.page", "End ProcessPostData Second Try");
  46. Trace.Write( "aspx.page", "Begin Raise ChangedEvents");
  47. }
  48. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_ENTER, _context.WorkerRequest);
  49. RaiseChangedEvents(); //Page页面生命周期 RaiseChangedEvents阶段(处理回发更改事件)
  50. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_POST_DATA_CHANGED_LEAVE, _context.WorkerRequest);
  51. if (con.TraceIsEnabled) {
  52. Trace.Write( "aspx.page", "End Raise ChangedEvents");
  53. Trace.Write( "aspx.page", "Begin Raise PostBackEvent");
  54. }
  55. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_ENTER, _context.WorkerRequest);
  56. RaisePostBackEvent(_requestValueCollection); //Page页面生命周期 RaisePostBackEvent阶段(处理回发事件)
  57. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RAISE_POSTBACK_LEAVE, _context.WorkerRequest);
  58. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "End Raise PostBackEvent");
  59. }
  60. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "Begin LoadComplete");
  61. OnLoadComplete(EventArgs.Empty); //Page页面生命周期 加载完成
  62. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "End LoadComplete");
  63. if (IsPostBack && IsCallback) {
  64. PrepareCallback(callbackControlId); //Page页面生命周期 PrepareRender(预呈现)
  65. }
  66. else if (!IsCrossPagePostBack) {
  67. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "Begin PreRender");
  68. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_ENTER, _context.WorkerRequest);
  69. PreRenderRecursiveInternal();
  70. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_PRE_RENDER_LEAVE, _context.WorkerRequest);
  71. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "End PreRender");
  72. }
  73. }
  74. /// Async Point here
  75. if (_legacyAsyncInfo == null || _legacyAsyncInfo.CallerIsBlocking) {
  76. // for non-async pages with registered async tasks - run the tasks here
  77. // also when running async page via server.execute - run the tasks here
  78. ExecuteRegisteredAsyncTasks();
  79. }
  80. // Make sure RawUrl gets validated.
  81. ValidateRawUrlIfRequired();
  82. if (includeStagesAfterAsyncPoint) {
  83. if (IsCallback) {
  84. RenderCallback();
  85. return;
  86. }
  87. if (IsCrossPagePostBack) {
  88. return;
  89. }
  90. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "Begin PreRenderComplete");
  91. PerformPreRenderComplete();
  92. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "End PreRenderComplete");
  93. if (con.TraceIsEnabled) {
  94. BuildPageProfileTree(EnableViewState);
  95. Trace.Write( "aspx.page", "Begin SaveState");
  96. }
  97. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_ENTER, _context.WorkerRequest);
  98. SaveAllState(); //Page页面生命周期 SaveAllState阶段(保存视图状态)
  99. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_SAVE_VIEWSTATE_LEAVE, _context.WorkerRequest);
  100. if (con.TraceIsEnabled) {
  101. Trace.Write( "aspx.page", "End SaveState");
  102. Trace.Write( "aspx.page", "Begin SaveStateComplete");
  103. }
  104. OnSaveStateComplete(EventArgs.Empty);
  105. if (con.TraceIsEnabled) {
  106. Trace.Write( "aspx.page", "End SaveStateComplete");
  107. Trace.Write( "aspx.page", "Begin Render");
  108. }
  109. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_ENTER, _context.WorkerRequest);
  110. // Special-case Web Part Export so it executes in the same security context as the page itself (VSWhidbey 426574)
  111. if (exportedWebPartID != null) {
  112. ExportWebPart(exportedWebPartID);
  113. }
  114. else {
  115. RenderControl(CreateHtmlTextWriter(Response.Output)); //Page页面生命周期 Render阶段(呈现)
  116. }
  117. if (EtwTrace.IsTraceEnabled(EtwTraceLevel.Verbose, EtwTraceFlags.Page)) EtwTrace.Trace(EtwTraceType.ETW_TYPE_PAGE_RENDER_LEAVE, _context.WorkerRequest);
  118. if (con.TraceIsEnabled) Trace.Write( "aspx.page", "End Render");
  119. CheckRemainingAsyncTasks( false);
  120. }
  121. }


至此,整个IIS处理Asp.Net完整请求就结束了,列举了各个关键处理阶段的处理函数。当我们了解.net framework内部处理机制后能够使我们理解Asp.Net运行机制和本质,对开发人员来说这很重要,不能只停留在会用会写的阶段,而且要知道其内部原理。这样不仅可以帮助我们编写更高效的代码,同时可以学习并借鉴微软的面向对象的编程思想,受益颇多!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值