Today I saw the life cycle of Web requests, and after reading it some do not understand, is to use the Anti-compilation tool to view the internal implementation of the package inside the library.
Web.dll from inside the computer, use the Anti-compilation tool to open
After opening
Public intProcessRequest (IntPtr ECB,intIwrtype) {INTPTR IntPtr=IntPtr.Zero; if(Iwrtype = =2) {intPtr=ECB; ECB=UNSAFENATIVEMETHODS.GETECB (INTPTR); }
//Create the Isapiworkerrequest first, encapsulate the request packet, call the Createworkerrequest method to implement, Isapiworkerrequest isapiworkerrequest=NULL; intresult; Try { BOOLUseoop = Iwrtype = =1; Isapiworkerrequest=isapiworkerrequest.createworkerrequest (ECB, useoop); Isapiworkerrequest.initialize (); stringapppathtranslated =isapiworkerrequest.getapppathtranslated (); stringAppdomainapppathinternal =httpruntime.appdomainapppathinternal; if(Appdomainapppathinternal = =NULL||Stringutil.equalsignorecase (apppathtranslated, appdomainapppathinternal)) {
//Call the Processrequestnodemand method and pass the encapsulated request message in Httpruntime.processrequestnodemand (isapiworkerrequest); Result=0; } Else{Httpruntime.shutdownappdomain (applicationshutdownreason.physicalapplicationpathch Anged, 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 isThreadAbortException) {Thread.resetabort (); } result=0; } returnresult; }
Create different objects within createworkerrequest based on different versions of IIS
// System.Web.Hosting.ISAPIWorkerRequest
Internal Static 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=NewIsapiworkerrequestoutofproc (ECB); } Else { intnum = unsafenativemethods.ecbgetversion (ECB) >> -; 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=NewISAPIWorkerRequestInProcForIIS7 (ECB); } Else { if(num = =6) {result=NewISAPIWorkerRequestInProcForIIS6 (ECB); } Else{result=NewIsapiworkerrequestinproc (ECB); } } } returnresult;}
Enter Processrequestnodemand interior
//System.Web.HttpRuntimeInternal Static voidProcessrequestnodemand (HttpWorkerRequest wr) {Requestqueue Requestqueue=Httpruntime._theruntime._requestqueue; Url Updateinitialcounters (); if(Requestqueue! =NULL) {WR=Requestqueue.getrequesttoexecute (WR); } if(WR! =NULL) {Httpruntime.calculatewaittimeandupdateperfcounter (WR); Url Resetstarttime ();
//Call the Processrequestnow method and pass the encapsulated request message into the Httpruntime.processrequestnow (WR); }}
// System.Web.HttpRuntime Internal Static void Processrequestnow (HttpWorkerRequest wr) { httpruntime._theruntime.processrequestinternal (WR);}
Go inside the ProcessRequestInternal method
//System.Web.HttpRuntimePrivate voidprocessrequestinternal (HttpWorkerRequest wr) {interlocked.increment (ref This. _activerequestcount); if( This. _disposinghttpruntime) { Try{wr. Sendstatus (503,"Server Too Busy"); Url Sendknownresponseheader ( A,"text/html; Charset=utf-8"); byte[] bytes = Encoding.ASCII.GetBytes (""); byte[] expr_45 =bytes; Url Sendresponsefrommemory (expr_45, expr_45.length); Url Flushresponse (true); wr. Endofrequest (); } finally{interlocked.decrement (ref This. _activerequestcount); } return; } HttpContext HttpContext; Try {
//Create a HttpContext object based on the request message and return if an error is created 404
HttpContext=NewHttpContext (WR,false); } Catch { Try{wr. Sendstatus ( -,"Bad Request"); wr. Sendknownresponseheader ( A,"text/html; Charset=utf-8"); byte[] Bytes2 = Encoding.ASCII.GetBytes (""); 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 {
//Set the first request to false This. Ensurefirstrequestinit (HttpContext); } Catch { if(!httpContext.Request.IsDebuggingRequest) {Throw; } }
//Initialize Response httpContext.Response.InitResponseWriter ();
//Create application instances based on HttpApplicationFactory IHttpHandler applicationinstance=Httpapplicationfactory.getapplicationinstance (HttpContext); if(ApplicationInstance = =NULL) { Throw NewHttpException (SR. GetString ("Unable_create_app_object")); } if(Etwtrace.istraceenabled (5,1) {etwtrace.trace (Etwtracetype.etw_type_start_handler, Httpcontext.workerrequest, Applicationinst Ance. GetType (). FullName,"Start"); } if(applicationinstance isIHttpAsyncHandler) {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); }}
Enter Ensurefirstrequestinit
//System.Web.HttpRuntimePrivate voidEnsurefirstrequestinit (HttpContext context) {if( This. _beforefirstrequest) { BOOLFlag =false; Try{Monitor.Enter ( This,refflag); if( This. _beforefirstrequest) { //Set the first request to false This. _firstrequeststarttime =Datetime.utcnow; This. Firstrequestinit (context); This. _beforefirstrequest =false; Context. Firstrequest=true; } } finally { if(flag) {Monitor.Exit ( This); } } }}
There are 26 methods in application, there are 19 request pipeline events from top to bottom
Isapiruntime parsing in ASP. NET request processing mechanism