MVC系列(3) HttpRuntime详解分析(下)2015-01-08文章内容话说,经过各种各样复杂的我们不知道的内部处理,非托管代码正式开始调用ISPAIRuntime的 ProcessRequest方法了(ISPAIRuntime继承了IISPAIRuntime接口,该接口可以和COM进行交互,并且暴露了 ProcessRequest接口方法)。至于为什么要调用这个方法,大叔也不太清楚,找不到微软相关的资料哦。但大叔确定该方法就是我们进入 HttpRuntime的正式大门,接着看吧。
public int ProcessRequest(IntPtr ecb, int iWRType) {IntPtr pHttpCompletion = IntPtr.Zero;if (iWRType == WORKER_REQUEST_TYPE_IN_PROC_VERSION_2) {pHttpCompletion = ecb;ecb = UnsafeNativeMethods.GetEcb(pHttpCompletion);}ISAPIWorkerRequest wr = null;try {bool useOOP = (iWRType == WORKER_REQUEST_TYPE_OOP);wr = ISAPIWorkerRequest.CreateWorkerRequest(ecb, useOOP);wr.Initialize(); // check if app path matches (need to restart app domain?)String wrPath = wr.GetAppPathTranslated();String adPath = HttpRuntime.AppDomainAppPathInternal; if (adPath == null ||StringUtil.EqualsIgnoreCase(wrPath, adPath)) { HttpRuntime.ProcessRequestNoDemand(wr);return 0;}else {// need to restart app domainHttpRuntime.ShutdownAppDomain(ApplicationShutdownReason.PhysicalApplicationPathChanged,SR.GetString(SR.Hosting_Phys_Path_Changed,adPath,wrPath));return 1;}}catch(Exception e) {try {WebBaseEvent.RaiseRuntimeError(e, this);} catch {} // Have we called HSE_REQ_DONE_WITH_SESSION?If so, don"t re-throw.if (wr != null && wr.Ecb == IntPtr.Zero) {if (pHttpCompletion != IntPtr.Zero) {UnsafeNativeMethods.SetDoneWithSessionCalled(pHttpCompletion);}// if this is a thread abort exception, cancel the abortif (e is ThreadAbortException) {Thread.ResetAbort();}// IMPORTANT: if this thread is being aborted because of an AppDomain.Unload,// the CLR will still throw an AppDomainUnloadedException. The native caller// must special case COR_E_APPDOMAINUNLOADED(0x80131014) and not// call HSE_REQ_DONE_WITH_SESSION more than once.return 0;} // re-throw if we have not called HSE_REQ_DONE_WITH_SESSIONthrow;}}