Asp. NET Pipeline and application life cycle
ASPPipeline and application life cycle
Section 8.1 Describes the system architecture of IIS and the overall process of HTTP request processing, where you can know that each ASP. NET site corresponds to a Web application that responds to HTTP requests and provides the user with the information they need. So, ASP. NET application specifically responds to HTTP requests? What are the specific processing processes? This involves the life cycle of an ASP.
8.2.1 ASP. NET Application life cycle *
This section takes IIS 6 as an example to step through the process of processing HTTP requests by an ASP. The process of IIS 7 is slightly different than IIS 6, but is generally consistent.
1 The browser makes an HTTP request to access an ASP.
Assume that this request is the first request for the ASP. NET application to which this page belongs.
when this request arrives at the Web server, it is the responsibility of HTTP. SYS to receive it and pass it to the application pool corresponding to this ASP. The worker process running in this application pool is responsible for processing the request, based on the Url,http.sys of the request [1] .
After the worker process receives this request, it loads an ISAPI extension "Aspnet_isapi.dll" that is dedicated to processing the ASP. NET page and passes the HTTP request to it.
after the worker process loads the aspnet_isapi.dll, the aspnet_isapi.dll is responsible for loading the operating environment of the ASP. CLR [2] .
Worker processes work in an unmanaged environment (referred to as the Windows operating system itself), while objects in. NET work in a managed environment (referred to as the CLR), aspnet_ Isapi.dll played a bridge between the two, sending incoming HTTP requests (from an unmanaged environment) to the appropriate. NET objects (in a managed environment) for processing.
2 Creating Applicationmanager objects and application domains
After the CLR is loaded, the Applicationmanager class is responsible for creating an application domain. Every ASP. NET application runs in its own application domain and is identified by a unique application identifier.
Each application domain corresponds to an instance of the Applicationmanager class, which is responsible for managing the ASP. NET applications running in the domain (such as starting and stopping an ASP. NET application, creating objects in a specified ASP. NET application, and so on).
3 Creating a Hostingenvironment object
While creating an application domain for an ASP. NET application, a Hostingenvironment object is created that provides some management information (such as the identity of the ASP. NET application, the corresponding virtual directory, and the physical directory) of the ASP, and provides some additional functionality (such as Register an object in the application domain, impersonate a specific user, etc.).
4 Create an ASP. NET core object for each request
When the application domain creation is complete, a Isapiruntime object is created and automatically calls its ProcessRequest () method. In this method, the Isapiruntime object creates an HttpWorkerRequest object based on the incoming HTTP request, which wraps the various information of the HTTP request in an object-oriented manner (that is, The original HTTP request information is encapsulated as a HttpWorkerRequest object ). The Startprocessing () method of the Isapiruntime object is then invoked to start the entire HTTP request processing process (This is "http pipeline: http Pipeline"), At the beginning of this process, an object of type httpruntime is created, and the previously created HttpWorkerRequest object is passed as a method parameter to the ProcessRequest () method of this HttpRuntime object.
Some very important work has been done in the ProcessRequest () method of the HttpRuntime class, where the most closely related to Web software engineers are:
The ProcessRequest () method of the HttpRuntime class creates a HttpContext object based on the HTTP request information provided in the HttpWorkerRequest object.
The HttpContext object is important because this object contains another two objects that are very common in asp:HttpResponse and HttpRequest.
The information in the HttpRequest object is derived from the original HTTP request, such as its URL attribute representing the URL in the original HTTP request information.
The HttpResponse object, however, has properties and methods for generating information to be returned to the browser.
The page class provides properties to reference these two objects, so you can access both objects directly using the "Requset" and "Response" properties in the ASP. For example:
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
Response.Write (Request.url);
}
}
The context property of the page class references the HttpContext object, so the above code can also be rewritten as follows:
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
This. Context.Response.Write (this. CONTEXT.REQUEST.URL);
}
}
With regard to the three objects of HttpContext, HttpResponse and HttpRequest, the following points must be mastered:
l HttpContext objects contain httpresponse and httprequest objects, which can be The httprequest object gets information about the HTTP request, and the content to be output to the browser can be called by the HttpResponse implementation of the method.
for each HTTP request,ASP . NET creates a HttpContext object, and the entire This object can be accessed during HTTP processing.
5 Assigning a HttpApplication object for processing requests
the ProcessRequest () method of the HttpRuntime class, in addition to creating HttpContext objects, also accomplishes another important task--to an instance of the HttpApplicationFactory class [3] the application assigns a HttpApplication object to manage various events in the entire HTTP request processing pipeline.
The HttpApplicationFactory object is responsible for managing a pool of HttpApplication objects [4] , when an HTTP request arrives, if there are HttpApplication objects available in the pool, the object is allocated directly for processing the HTTP request, otherwise a new HttpApplication object is created.
6 HttpApplication object launches HTTP pipeline
The HttpApplication object is responsible for assembling the entireHTTP Request processing pipeline (HTTP Pipeline), which makes an analogy between the HTTP request processing pipeline and the production line in a modern factory. The HttpContext object created in the previous step is the "product" to be machined in this production line, and when it flows through different parts of the "production line", it will be processed and processed in a specific way.
How are these specific "processing and processing processes" carried out?
Simply put, the HttpContext object passes through different parts of the "production line", and the HttpApplication object fires a sequence of events [5]
The HTTP Module object is the Initmodules () method in the HttpApplication object [6] in the HTTP Module Object init () method that we typically create in [7] write code in to respond to specific events that are fired by the HttpApplication object.
ASP. NET provides a number of predefined HTTP modules to respond to specific events, and Web software engineers can also write their own HTTP modules and insert them into the HTTP request processing pipeline [8].
In the middle of the pipeline (the related event is processed), the HttpContext object is received by the final Page object (this is why the HttpContext object can be accessed through the context property defined by the page class in an ASP. NET page).
Each of the visited ASP. NET pages is converted to a page class derived from the page class.
Note: The page class implements the IHttpHandler interface, which defines a ProcessRequest () method.
Asp. NET page class is generated and then automatically compiled into an assembly, and its ProcessRequest () method is automatically called (because the page class implements the IHttpHandler interface, so there must be this method). In this method, the code written by the Web software engineer is executed (if any). The execution result of the ProcessRequest () method is again hosted by the HttpContext object, and the control is redirected back to the HTTP request processing pipeline, and the HttpApplication object continues to fire subsequent events. At this point, if there are specific HTTP modules that respond to these events, they are automatically called.
HttpContext object with the final processing results came to the "HTTP request processing pipeline" of the end, its information is taken out, again to Aspnet_isapi.dll as a bridge to the worker process. The worker process then forwards the processing result of the HTTP request to the. SYS, which is responsible for returning the results to the browser.
According to the previous introduction, the entire HTTP pipeline can be divided into three segments: preprocessing stage à processing stage à post-processing stage (Figure 8?14).
Figure 8?14 Three stages of an HTTP pipeline
As shown in 8?14, the preprocessing and post-processing phases of the HTTP pipeline are mainly involved by multiple HTTP modules and are driven by events, and the work done in these two phases is mainly to modify the various properties of the HttpContext object.
The process of processing an HTTP request is ultimately done by an object that implements the IHttpHandler interface in the processing phase. This interface is implemented for each of the page classes generated by an ASP. The work of creating an appropriate HTTP request processing object is done by the PageHandlerFactory object [9] .
Thus, the object that implements the IHttpHandler interface is responsible for handling HTTP requests, which is why it is called a "Handler (handler)".
In addition to the most common ASP. NET Web pages, Web software engineers can create their own objects that implement the IHttpHandler interface and insert them into the HTTP pipeline for processing HTTP requests.
When the HTTP request is processed, the related object is disposed, but the created application domain, and the HttpApplication, and so on, remain alive in response to the next HTTP request.
7 ASP. NET application life cycle Summary
This section describes the life cycle of an ASP, which is a fairly complex process that may be easier to understand with the following popular analogy:
L "HTTP Request processing Pipeline" is a modern factory in the "production line",HttpContext object is this line of products to be processed.
The HttpHandler(HTTP Handler) object is the core of the entire "product line", which is responsible for forming the product assembly.
L HttpModule(HTTP module) is equivalent to the "production line" of auxiliary workers, they are products (HttpContext Objects) "pretreatment" (for assembly Products) and "post-processing" (for the product factory preparation, such as the labeling of trademarks).
The HttpApplication object is the "leader" of the entire "production line", which assigns workers to the "production line" (initializes and loads all registered HttpModule) and then fires a series of events (called "asp.ne T Application Events "), specific HttpModule is responsible for responding to specific events.
[1] If the worker process does not exist, IIS Monitor was will create one, otherwise, reuse the existing worker process.
[2] In the case of IIS 7 in Integrated mode, this step is not required because the CLR is preloaded.
[3] An instance of a class is equivalent to the object of a class, and all refers to an object created with a class as a template.
[4] Object pool is a common object organization for object-oriented software systems and can be viewed as an object container. The object pool is placed with multiple objects that have been created well in advance. When an object is needed by the outside world, a ready-made object can be taken directly from the pool, which avoids the performance penalty of creating objects frequently.
[5] HttpApplication defines a considerable number of events, complete with a list of events, and see MSDN.
[6] This method is called automatically when the HttpApplication object is acquired.
[7] all HTTP modules implement the IHttpModule interface, and the Init () method is defined by this interface.
[8] by inserting a specific content in Web. config, you can add a custom HTTP module to the process of the HTTP request.
[9] This is another core class in the ASP. NET Technology framework.
*******************************************
This series of articles concluding remarks:
Understanding HTTP pipeline is important in ASP, and it is understood only to understand the problems encountered in development and to lay the groundwork for learning and mastering more complex web development techniques, such as custom HttpModule and HttpHandler.
So far, a series of articles on the principles of ASP. NET Web programming is finished. This is only part of the reason I found that many of the ASP. NET technical books are vague to this part of the story, and this part is very important, I hope these four articles can be helpful to everyone. Most of the other regular content is available in the vast majority of ASP.
If there are errors and omissions in this article, also begged the master pointed out.
I wish you all a smooth study.
Asp. NET Pipeline and application life cycle