ASP.NET从源码看实现(四):HttpApplication

IIS处理流程:HttpApplication

     当HttpApplication实体从HttpApplicationFactory工厂中创建并返会到HttpRuntime后,开始执行HttpApplication.BeginProcessRequest(context, this._handlerCompletionCallback, context);进行异步回调执行ProcessRequest,从这里开始正式进入到Request的请求处理阶段;

        
         //HttpApplication
         IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
         {
            this._context = context;
            this._context.ApplicationInstance = this;
            this._stepManager.InitRequest();
            this._context.Root();
            HttpAsyncResult result = new HttpAsyncResult(cb, extraData);
            this.AsyncResult = result;
            if (this._context.TraceIsEnabled)
            {
                HttpRuntime.Profile.StartRequest(this._context);
            }
            this.ResumeSteps(null);
            return result;
        }
        private void ResumeSteps(Exception error)
        {
            this._stepManager.ResumeSteps(error);
        } 
// internal class ApplicationStepManager : HttpApplication.StepManager [DebuggerStepperBoundary] internal override void ResumeSteps(Exception error) { bool flag = false; bool completedSynchronously = true; HttpApplication application = base._application; CountdownTask applicationInstanceConsumersCounter = application.ApplicationInstanceConsumersCounter; HttpContext context = application.Context; ThreadContext context2 = null; AspNetSynchronizationContextBase syncContext = context.SyncContext; try { if (applicationInstanceConsumersCounter != null) { applicationInstanceConsumersCounter.MarkOperationPending(); } using (syncContext.AcquireThreadLock()) { try { context2 = application.OnThreadEnter(); } catch (Exception exception) { if (error == null) { error = exception; } } try { try { Label_004D: if (syncContext.Error != null) { error = syncContext.Error; syncContext.ClearError(); } if (error != null) { application.RecordError(error); error = null; } if (!syncContext.PendingCompletion(this._resumeStepsWaitCallback)) { if ((this._currentStepIndex < this._endRequestStepIndex) && ((context.Error != null) || base._requestCompleted)) { context.Response.FilterOutput(); this._currentStepIndex = this._endRequestStepIndex; } else { this._currentStepIndex++; } if (this._currentStepIndex >= this._execSteps.Length) { flag = true; } else { this._numStepCalls++; syncContext.Enable(); error = application.ExecuteStep(this._execSteps[this._currentStepIndex], ref completedSynchronously); if (completedSynchronously) { this._numSyncStepCalls++; goto Label_004D; } } } } finally { if (flag) { context.RaiseOnRequestCompleted(); } if (context2 != null) { try { context2.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(); } } }

 

      其中error = application.ExecuteStep(this._execSteps[this._currentStepIndex], ref completedSynchronously); 便是通过_execSteps来依次执行事件函数的调用。

  在所有事件函数被调用完成之后,HttpApplication实例会被回收,ISAPIRuntime.ProcessRequest处理完毕,结果返回给COM,并通过COM的再一次处理,返回给客户端。这样一次请求就至此结束了。

整个过程的流程路线图如下:

 

 

posted @ 2014-11-25 11:29  飞蛾扑火  阅读(318)  评论(0编辑  收藏  举报