Asp.Net请求响应过程
在之前,我们写了自己的Asp.Net框架,对整个流程有了一个大概的认识。这次我们来看一下Asp.Net整个请求处理过程是怎么样的。
浏览器封装请求报文,发送请求到达服务器,服务器内核模块的HTTP.SYS监听到用户的HTTP请求,将其分发给W3SVC,W3SVC解析出请求的URL,并根据Metabase获取映射关系得到目标应用,如果是静态资源(HTML,jss,img等),则将内容以HTTP响应的车型是返回。如果是动态文件(aspx、ashx)等,则进一步获取到目标对应的工作进程w3wp.exe,如果不存在,则创建一个新的工作进程。工作进程w3wp.exe利用aspnet_isapi.dll创建处理当前请求的应用程序域,随后ISAPIRuntime会被加载,ISAPIRuntime会接管该HTTP请求。
ISAPIRuntime会首先创建一个ISAPIWorkRequest对象,对请求报文进行了简单的封装,并将该ISAPIWorkRequest对象传递给HttpRuntime。
HttpRuntime会根据ISAPIWorkRequest创建用于封装Http请求上下文的对象HttpConetxt。
HttpContext主要包括HttpRequest(当前请求)和HttpResponse(服务器响应)
ISAPIRuntime
[SecurityPermission(SecurityAction.LinkDemand, Unrestricted=true)]
public int ProcessRequest(IntPtr ecb, int iWRType)
{
IntPtr zero = IntPtr.Zero;
if (iWRType == 2)
{
zero = ecb;
ecb = UnsafeNativeMethods.GetEcb(zero);
}
//创建了ISAPIWorkRquest空对象
ISAPIWorkerRequest wr = null;
try
{
bool useOOP = iWRType == 1;
//通过ecb句柄创建了ISAPIWorkRequest对象
wr = ISAPIWorkerRequest.CreateWorkerRequest(ecb, useOOP);
wr.Initialize();
string appPathTranslated = wr.GetAppPathTranslated();
string appDomainAppPathInternal = HttpRuntime.AppDomainAppPathInternal;
if ((appDomainAppPathInternal == null) || StringUtil.EqualsIgnoreCase(appPathTranslated, appDomainAppPathInternal))
{
//IsapiRuntime把WR交给了HttpRuntime
HttpRuntime.ProcessRequestNoDemand(wr);
return 0;
}
HttpRuntime.ShutdownAppDomain(ApplicationShutdownReason.PhysicalApplicationPathChanged, SR.GetString("Hosting_Phys_Path_Changed", new object[] { appDomainAppPathInternal, appPathTranslated }));
return 1;
}
catch (Exception exception)
{
try
{
WebBaseEvent.RaiseRuntimeError(exception, this);
}
catch
{
}
if ((wr == null) || !(wr.Ecb == IntPtr.Zero))
{
throw;
}
if (zero != IntPtr.Zero)
{
UnsafeNativeMethods.SetDoneWithSessionCalled(zero);
}
if (exception is ThreadAbortException)
{
Thread.ResetAbort();
}
return 0;
}
}
ISAPIRuntime
HttpRuntime通过HttpApplicationFactory获取一个新的或现有的HttpApplication对象。
HttpRuntime
private void ProcessRequestInternal(HttpWorkerRequest wr)
{
Interlocked.Increment(ref this._activeRequestCount);
if (this._disposingHttpRuntime)
{
try
{
wr.SendStatus(0x1f7, "Server Too Busy");
wr.SendKnownResponseHeader(12, "text/html; charset=utf-8");
byte[] bytes = Encoding.ASCII.GetBytes("<html><body>Server Too Busy</body></html>");
wr.SendResponseFromMemory(bytes, bytes.Length);
wr.FlushResponse(true);
wr.EndOfRequest();
}
finally
{
Interlocked.Decrement(ref this._activeRequestCount);
}
}
else
{
HttpContext context;
try
{
//通过wr创建了上下文对象
context = new HttpContext(wr, false);
}
catch
{
try
{
wr.SendStatus(400, "Bad Request");
wr.SendKnownResponseHeader(12, "text/html; charset=utf-8");
byte[] data = Encoding.ASCII.GetBytes("<html><body>Bad Request</body></html>");
wr.SendResponseFromMemory(data, data.Length);
wr.FlushResponse(true);
wr.EndOfRequest();
return;
}
finally
{
Interlocked.Decrement(ref this._activeRequestCount);
}
}
wr.SetEndOfSendNotification(this._asyncEndOfSendCallback, context);
HostingEnvironment.IncrementBusyCount();
try
{
try
{
this.EnsureFirstRequestInit(context);
}
catch
{
if (!context.Request.IsDebuggingRequest)
{
throw;
}
}
context.Response.InitResponseWriter(); //通过HttpApplicationFactory获取HttpApplication实例
IHttpHandler applicationInstance = HttpApplicationFactory.GetApplicationInstance(context);
if (applicationInstance == null)
{
throw new HttpException(SR.GetString("Unable_create_app_object"));
}
if (EtwTrace.IsTraceEnabled(5, 1))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, context.WorkerRequest, applicationInstance.GetType().FullName, "Start");
}
if (applicationInstance is IHttpAsyncHandler)
{
IHttpAsyncHandler handler2 = (IHttpAsyncHandler) applicationInstance;
context.AsyncAppHandler = handler2;
//执行HttpApplication的BeginProcessRequest方法
handler2.BeginProcessRequest(context, this._handlerCompletionCallback, context);
}
else
{
applicationInstance.ProcessRequest(context);
this.FinishRequest(context.WorkerRequest, context, null);
}
}
catch (Exception exception)
{
context.Response.InitResponseWriter();
this.FinishRequest(wr, context, exception);
}
}
}
HttpRuntime
HttpApplication负责处理分发给它的请求。由于一个HttpApplication对象在同一时间只能处理一个请求,只有完成对某个请求的处理后,HttpApplication才能用于后续请求 的处理,所以Asp.Net采用对象池的获取HttpApplication对象。
当第一个请求到达时,HttpApplicationFactory会一次创建多个HttpApplication对象,并将其置于对象池中,选择其中一个对象来处理该请求。处理完毕后HttpApplication不会回收,而是释放到池中。池中空闲的HttpApplication对象用于处理后续请求,当没有空闲的时候再进行创建新的HttpApplication。
HttpApplication对象的创建时根据Global文件编译后的类型,通过反射的方式创建的,很消耗资源和时间,因此这里使用了对象池的技术
1.确保Global文件被编译,如果没有Global文件,则对所有事件提供HttpApplication的默认行为。
Global:它继承自HttpApplication类,用于维护一个HttpApplication对象池,并在需要的时候讲对象池中的对象分配给应用程序。
2.创建一个特殊的HttpApplication对象,并调用它的Application_Start方法。
3.获取一个用于处理当前请求的HttpApplication实例,进行初始化。
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);
}
初始化:①.创建HttpModuleCollection集合——>根据配置文件,获取所有的HttpModule,并循环执行HttpModule的初始化方法。
private void InitModules()
{
HttpModuleCollection modules = RuntimeConfig.GetAppConfig().HttpModules.CreateModules();
HttpModuleCollection other = this.CreateDynamicModules();
modules.AppendCollection(other);
this._moduleCollection = modules;
this.InitModulesCommon();
} private void InitModulesCommon()
{
int count = this._moduleCollection.Count;
for (int i = 0; i < count; i++)
{
this._currentModuleCollectionKey = this._moduleCollection.GetKey(i);
this._moduleCollection[i].Init(this);
}
this._currentModuleCollectionKey = null;
this.InitAppLevelCulture();
}
②.对HttpApplication的管道事件和HttpHandler有序绑定,主要有两处HttpHandler。
1.——>在管道第7-8个事件之间执行了MapHandlerExecutionStep事件:
判断是否指向了一个具体的HttpHandler实例,如果没有,则根据请求的url创建页面处理程序或一般处理程序。
(为什么要判断是否指向一个具体的HttpHandler呢,MVC请求到达的时候会在第七个事件中,指向一个MvcHandler,而不是创建一般处理程序)
2.——>在管道第11-12个事件之间执行了HttpHandler的ProcessRequest方法:
执行一般处理程序的ProceRequest方法,或者是Page页面的生命周期。
internal override void BuildSteps(WaitCallback stepCallback)
{
ArrayList steps = new ArrayList();
HttpApplication app = base._application;
bool flag = false;
UrlMappingsSection urlMappings = RuntimeConfig.GetConfig().UrlMappings;
flag = urlMappings.IsEnabled && (urlMappings.UrlMappings.Count > 0);
steps.Add(new HttpApplication.ValidateRequestExecutionStep(app));
steps.Add(new HttpApplication.ValidatePathExecutionStep(app));
if (flag)
{
steps.Add(new HttpApplication.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 HttpApplication.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());
//执行一般处理程序的PR方法或开始Page页面的生命周期
steps.Add(new HttpApplication.CallHandlerExecutionStep(app));
app.CreateEventExecutionSteps(HttpApplication.EventPostRequestHandlerExecute, steps);
app.CreateEventExecutionSteps(HttpApplication.EventReleaseRequestState, steps);
app.CreateEventExecutionSteps(HttpApplication.EventPostReleaseRequestState, steps);
steps.Add(new HttpApplication.CallFilterExecutionStep(app));
app.CreateEventExecutionSteps(HttpApplication.EventUpdateRequestCache, steps);
app.CreateEventExecutionSteps(HttpApplication.EventPostUpdateRequestCache, steps);
this._endRequestStepIndex = steps.Count;
app.CreateEventExecutionSteps(HttpApplication.EventEndRequest, steps);
steps.Add(new HttpApplication.NoopExecutionStep());
this._execSteps = new HttpApplication.IExecutionStep[steps.Count];
steps.CopyTo(this._execSteps);
this._resumeStepsWaitCallback = stepCallback;
}
HttpRuntime拿到了HttpApplication对象,HttpRuntime开始触发HttpApplication的请求处理,即调用HttpApplication的BeginProcessRequest方法。
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;
}
ResumeSteps
其中error = application.ExecuteStep(this._execSteps[this._currentStepIndex], ref completedSynchronously);
便是通过_execSteps来依次执行事件函数的调用。
在所有事件函数被调用完成之后,HttpApplication实例会被回收,ISAPIRuntime.ProcessRequest处理完毕,结果返回给COM,并通过COM的再一次处理,返回给客户端。这样一次请求就至此结束了。
[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();
}
}
}
HttpApplication 管线会依次处理下面的请求:
- 对请求进行验证,将检查浏览器发送的信息,并确定其是否包含潜在恶意标记。
- 如果已在 Web.config 文件的 UrlMappingsSection 节中配置了任何 URL,则执行 URL 映射。
- 引发 BeginRequest 事件。
- 引发 AuthenticateRequest 事件。
- 引发 PostAuthenticateRequest 事件。
- 引发 AuthorizeRequest 事件。
- 引发 PostAuthorizeRequest 事件。
- 引发 ResolveRequestCache 事件。
- 引发 PostResolveRequestCache 事件。通知HttpModule根据请求选择对应的HttpHnadler加载至上下文中。
- 根据所请求资源的文件扩展名(在应用程序的配置文件中映射),选择实现 IHttpHandler 的类,对请求进行处理。如果该请求针对从 Page 类派生的对象(页),并且需要对该页进行编译,则 ASP.NET 会在创建该页的实例之前对其进行编译。(除了配置文件中的,还有上下文中的HttpHnadler)
- 引发 PostMapRequestHandler 事件。继续通知HttpModule确定要使用哪个HttpHandler用以处理请求。
- 引发 AcquireRequestState 事件。
- 引发 PostAcquireRequestState 事件。
- 引发 PreRequestHandlerExecute 事件。
- 为该请求调用合适的 IHttpHandler 类的 ProcessRequest 方法(或异步版 BeginProcessRequest)。例如,如果该请求针对某页,则当前的页实例将处理该请求。
- 引发 PostRequestHandlerExecute 事件。
- 引发 ReleaseRequestState 事件。
- 引发 PostReleaseRequestState 事件。
- 如果定义了 Filter 属性,则执行响应筛选。
- 引发 UpdateRequestCache 事件。
- 引发 PostUpdateRequestCache 事件。
- 引发 EndRequest 事件。