When a request arrives, it is routed to isapiruntime. processrequest () method. this method calls httpruntime. processrequest method, which performs some important tasks (use reflector to view the system. web. httpruntime. processrequestinternal method ):
· Create a New httpcontext instance for the request
· Get an httpapplication instance
· Call httpapplication. INIT () to set the events of the MPs queue.
· The init () method triggers the httpapplication. resumeprocessing () method that starts ASP. NET pipeline processing.
First, a new httpcontext object is created and used to pass the isapiworkerrequest (the wrapper of the isapi ecb ). this context is always available throughout the request lifecycle and can always be accessed through the static attribute httpcontext. currect to access. as the name implies, the httpcontext object represents the context of the current active request because it contains all the typical important objects you need to access in the request lifecycle: request, response, application, server, cache. httpcontext. current gives you access to all these capabilities.
The httpcontext object also contains a very useful items set. You can use it to save data for specific requests. the context object is created at the beginning of the Request cycle and released at the end of the request. All data stored in the items set is only available in this specific request. A good example is the request log mechanism. the application_beginrequest and application_endrequest Methods attached to asax record the request start and end times (as shown in figure 3 ). httpcontext is very useful to you-if you need data in different parts of the request or page processing, you can use it freely.
List 3-use the httpcontext. Items collection to save data in different MPs queue events
Protected void application_beginrequest (Object sender, eventargs E)
{
// *** Request Logging
If (App. configuration. logwebrequests)
Context. Items. Add ("weblog_starttime", datetime. Now );
}
Protected void application_endrequest (Object sender, eventargs E)
{
// *** Request Logging
If (App. configuration. logwebrequests)
{
Try
{
Timespan span = datetime. Now. Subtract (datetime) Context. items ["weblog_starttime"]);
Int milisecs = span. totalmilliseconds;
// Do your logging
Webrequestlog. Log (App. configuration. connectionstring, true, millisecs );
}
}
}
Once the context is set, Asp. net needs to route the received requests to the appropriate application/virtual directory through the httpapplication object. each ASP.. Net Applications must be set to a virtual directory (or web root directory), and each "application" is processed separately.
Httpapplication is similar to the host of the ceremony-it is the place where the processing action begins.
Domain Master: httpapplication
Each request is routed to an httpapplication object. the httpapplicationfactory class serves your asp.. NET application creates an httpapplication Object pool and distributes references to the httpapplication object for each request. the size of the Object pool is affected by machine. the maxworkerthreads setting limit in the processmodel key in the config file. The default value is 20. This may be incorrect. According to the code decompiled by reflector, the pool size should be 100, if the pool size is less than 100, httpapplicationfactory will create 100 full tables, but considering that multiple threads will create httpapplication at the same time, the actual size may exceed 100 ).
The object pool starts with a smaller number. Generally, an object pool grows to the same number of requests that need to be processed at the same time. the object pool is monitored, so that it may increase to the maximum number of instances under a large load, and a smaller number will be returned when the load is reduced.
Httpapplication is the external wrapper of your web program, and it is mapped to global. class defined in asax. it is the first entry point to enter httpruntime. if you view global. asax (or the corresponding code class) you will find this
Classes are directly inherited from httpapplication:
Public class Global: system. Web. httpapplication
The main responsibility of httpapplication is to act as the event controller of the HTTP pipeline. Therefore, its interfaces mainly contain events. Event mounting is very extensive, including the following:
· Beginrequest
· Authenticaterequest
· Authorizerequest
· Resolverequestcache
· Aquirerequeststate
· Prerequesthandlerexecute
·... Handler execution...
· Postrequesthandlerexecute
· Releaserequeststate
· Updaterequestcache
· Endrequest
Each event is stored in global. an empty event prefixed with application _ in the assx file is implemented. for example, application_beginrequest (), application_authorizerequest ().. these processors are provided for ease of use because they are often used in programs, so you do not need to explicitly create these event processing delegates.
Understand Each ASP. net virtual directory runs in its own application domain, and there are multiple slave ASP. it is very important that the returned httpapplication instances in the pool managed by. net run at the same time. This is why multiple requests can be processed at the same time without interfering with each other.
View list 4 to obtain the relationship between the application domain, thread, and httpapplication.
List 4-shows the relationship between application domains, threads, and httpapplication instances.
Private void page_load (Object sender, system. eventargs E)
{
// Put user code to initialize the page here
This. applicationid = (howaspnetworks. Global) httpcontext. Current. applicationinstance). applicationid;
This. threadid = appdomain. getcurrentthreadid ();
This. domainid = appdomain. currentdomain. friendlyname;
This. threadinfo = "threadpool thread:" + system. Threading. thread. currentthread. isthreadpoolthread. tostring () + "<br> thread apartment:" +
System. Threading. thread. currentthread. apartmentstate. tostring ();
// *** Simulate a slow request so we can see multiple
// Requests side by side.
System. Threading. thread. Sleep (3000 );
}
This is part of the demo provided with the sample. The running result is shown in Figure 5. Run the two browsers. Open this demo page and you will see different IDs.
Figure 5-you can simply view the application domain, how the application pool instance interacts with the request thread by running multiple browsers at the same time. When multiple requests are initiated at the same time, you can see that the thread ID and Application ID have changed, but the application domain is the same.
You may notice that in most requests, the application domain id remains unchanged when the thread and httpapplication ID change, although they may also be repeated (referring to the thread and httpapplication ID ). httpapplication is taken from a collection,
It can be reused in subsequent requests, so its ID is sometimes repeated. note that application instances are not bound to specific threads-specifically, they are the active threads specified for the current request.