您的位置:首页 > 编程语言 > ASP

Asp.Net请求处理机制中IsApiRuntime解析

2016-01-15 14:58 696 查看
今天看了web请求的生命周期,看完了还有些不懂,就是用反编译工具,查看封装内库的内部实现。



从计算机内部查到web.dll,使用反编译工具打开



打开后

public int ProcessRequest(IntPtr ecb, int iWRType)
{
IntPtr intPtr = IntPtr.Zero;
if (iWRType == 2)
{
intPtr = ecb;
ecb = UnsafeNativeMethods.GetEcb(intPtr);
}
//首先创建ISAPIWorkerRequest,将请求报文封装在内,调用CreateWorkerRequest方法来实现,
ISAPIWorkerRequest iSAPIWorkerRequest = null;
int result;
try
{
bool useOOP = iWRType == 1;
iSAPIWorkerRequest = ISAPIWorkerRequest.CreateWorkerRequest(ecb, useOOP);
iSAPIWorkerRequest.Initialize();
string appPathTranslated = iSAPIWorkerRequest.GetAppPathTranslated();
string appDomainAppPathInternal = HttpRuntime.AppDomainAppPathInternal;
if (appDomainAppPathInternal == null || StringUtil.EqualsIgnoreCase(appPathTranslated, appDomainAppPathInternal))
{
//调用ProcessRequestNoDemand方法,并将封装的请求报文传入进去
HttpRuntime.ProcessRequestNoDemand(iSAPIWorkerRequest);
result = 0;
}
else
{
HttpRuntime.ShutdownAppDomain(ApplicationShutdownReason.PhysicalApplicationPathChanged, SR.GetString("Hosting_Phys_Path_Changed", new object[]
{
appDomainAppPathInternal,
appPathTranslated
}));
result = 1;
}
}
catch (Exception ex)
{
try
{
WebBaseEvent.RaiseRuntimeError(ex, this);
}
catch
{
}
if (iSAPIWorkerRequest == null || !(iSAPIWorkerRequest.Ecb == IntPtr.Zero))
{
throw;
}
if (intPtr != IntPtr.Zero)
{
UnsafeNativeMethods.SetDoneWithSessionCalled(intPtr);
}
if (ex is ThreadAbortException)
{
Thread.ResetAbort();
}
result = 0;
}
return result;
}


在CreateWorkerRequest内部,根据不同的IIS版本创建不同的对象

// System.Web.Hosting.ISAPIWorkerRequest

internal static ISAPIWorkerRequest CreateWorkerRequest(IntPtr ecb, bool useOOP)

{
ISAPIWorkerRequest result;
if (useOOP)
{
EtwTrace.TraceEnableCheck(EtwTraceConfigType.DOWNLEVEL, IntPtr.Zero);
if (EtwTrace.IsTraceEnabled(5, 1))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_APPDOMAIN_ENTER, ecb, Thread.GetDomain().FriendlyName, null, false);
}
result = new ISAPIWorkerRequestOutOfProc(ecb);
}
else
{
int num = UnsafeNativeMethods.EcbGetVersion(ecb) >> 16;
if (num >= 7)
{
EtwTrace.TraceEnableCheck(EtwTraceConfigType.IIS7_ISAPI, ecb);
}
else
{
EtwTrace.TraceEnableCheck(EtwTraceConfigType.DOWNLEVEL, IntPtr.Zero);
}
if (EtwTrace.IsTraceEnabled(5, 1))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_APPDOMAIN_ENTER, ecb, Thread.GetDomain().FriendlyName, null, true);
}
if (num >= 7)
{
result = new ISAPIWorkerRequestInProcForIIS7(ecb);
}
else
{
if (num == 6)
{
result = new ISAPIWorkerRequestInProcForIIS6(ecb);
}
else
{
result = new ISAPIWorkerRequestInProc(ecb);
}
}
}
return result;
}


进入ProcessRequestNoDemand内部

// System.Web.HttpRuntime
internal static void ProcessRequestNoDemand(HttpWorkerRequest wr)
{
RequestQueue requestQueue = HttpRuntime._theRuntime._requestQueue;
wr.UpdateInitialCounters();
if (requestQueue != null)
{
wr = requestQueue.GetRequestToExecute(wr);
}
if (wr != null)
{
HttpRuntime.CalculateWaitTimeAndUpdatePerfCounter(wr);
wr.ResetStartTime();
//调用ProcessRequestNow方法,并将封装的请求报文传入
HttpRuntime.ProcessRequestNow(wr);
}
}


// System.Web.HttpRuntime
internal static void ProcessRequestNow(HttpWorkerRequest wr)
{
HttpRuntime._theRuntime.ProcessRequestInternal(wr);
}


进入ProcessRequestInternal方法内部

// System.Web.HttpRuntime
private void ProcessRequestInternal(HttpWorkerRequest wr)
{
Interlocked.Increment(ref this._activeRequestCount);
if (this._disposingHttpRuntime)
{
try
{
wr.SendStatus(503, "Server Too Busy");
wr.SendKnownResponseHeader(12, "text/html; charset=utf-8");
byte[] bytes = Encoding.ASCII.GetBytes("<html><body>Server Too Busy</body></html>");
byte[] expr_45 = bytes;
wr.SendResponseFromMemory(expr_45, expr_45.Length);
wr.FlushResponse(true);
wr.EndOfRequest();
}
finally
{
Interlocked.Decrement(ref this._activeRequestCount);
}
return;
}
HttpContext httpContext;
try
{
//根据请求报文创建HttpContext对象,如果创建出错误就会返回404
httpContext = new HttpContext(wr, false);
}
catch
{
try
{
wr.SendStatus(400, "Bad Request");
wr.SendKnownResponseHeader(12, "text/html; charset=utf-8");
byte[] bytes2 = Encoding.ASCII.GetBytes("<html><body>Bad Request</body></html>");
byte[] expr_A5 = bytes2;
wr.SendResponseFromMemory(expr_A5, expr_A5.Length);
wr.FlushResponse(true);
wr.EndOfRequest();
return;
}
finally
{
Interlocked.Decrement(ref this._activeRequestCount);
}
}
wr.SetEndOfSendNotification(this._asyncEndOfSendCallback, httpContext);
HostingEnvironment.IncrementBusyCount();
try
{
try
{
//将第一次请求设置为false
this.EnsureFirstRequestInit(httpContext);
}
catch
{
if (!httpContext.Request.IsDebuggingRequest)
{
throw;
}
}
//初始化Response
httpContext.Response.InitResponseWriter();
//根据HttpApplicationFactory创建Application实例
IHttpHandler applicationInstance = HttpApplicationFactory.GetApplicationInstance(httpContext);
if (applicationInstance == null)
{
throw new HttpException(SR.GetString("Unable_create_app_object"));
}
if (EtwTrace.IsTraceEnabled(5, 1))
{
EtwTrace.Trace(EtwTraceType.ETW_TYPE_START_HANDLER, httpContext.WorkerRequest, applicationInstance.GetType().FullName, "Start");
}
if (applicationInstance is IHttpAsyncHandler)
{
IHttpAsyncHandler httpAsyncHandler = (IHttpAsyncHandler)applicationInstance;
httpContext.AsyncAppHandler = httpAsyncHandler;
httpAsyncHandler.BeginProcessRequest(httpContext, this._handlerCompletionCallback, httpContext);
}
else
{
applicationInstance.ProcessRequest(httpContext);
this.FinishRequest(httpContext.WorkerRequest, httpContext, null);
}
}
catch (Exception e)
{
httpContext.Response.InitResponseWriter();
this.FinishRequest(wr, httpContext, e);
}
}


//进入EnsureFirstRequestInit

// System.Web.HttpRuntime
private void EnsureFirstRequestInit(HttpContext context)
{
if (this._beforeFirstRequest)
{
bool flag = false;
try
{
Monitor.Enter(this, ref flag);
if (this._beforeFirstRequest)
{

//设置第一次请求为false
this._firstRequestStartTime = DateTime.UtcNow;
this.FirstRequestInit(context);
this._beforeFirstRequest = false;
context.FirstRequest = true;
}
}
finally
{
if (flag)
{
Monitor.Exit(this);
}
}
}
}


Application中有26个方法,从上到下有19个请求管道事件

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: