Asp. NET pipeline

Source: Internet
Author: User
Tags httpcontext

In IIS 6.0, for example, in worker process w3wp.exe, use Aspnet_ispai.dll to load the. NET runtime (if. NET run fashion is not loaded). IIS 6 introduces the concept of an application pool, a worker process that corresponds to an application pool. One application pool can host one or more web apps, and each Web app maps to an IIS virtual directory. As with IIS 5.x, each Web app runs in its own application domain.

If HTTP. SYS received the first access to the Web app, when the runtime is successfully loaded, an application domain (AppDomain) is created for the Web App by Appdomainfactory. Subsequently, a special run-time isapiruntime is loaded. Isapiruntime is defined in assembly system.web, and the corresponding namespace is System.Web.Hosting. Isapiruntime will take over the HTTP request.

Isapiruntime will first create a Isapiworkerrequest object that encapsulates the current HTTP request, The Isapiworkerrequest object is passed to the ASP. NET Runtime: HttpRuntime, from this point on, the HTTP request formally enters the ASP. Based on the Isapiworkerrequest object, HttpRuntime creates a context object to represent the current HTTP request: HttpContext.

As HttpContext is successfully created, HttpRuntime uses HttpApplicationFactory to create new or acquire existing HttpApplication objects. In fact, ASP. NET maintains a pool of HttpApplication objects, HttpApplicationFactory Select available HttpApplication users from the pool to process HTTP requests, and then release them to the object pool when processing is complete. The httpapplicationfactory is responsible for processing the current HTTP request.

During HttpApplication initialization, the corresponding HttpModule object is loaded and initialized according to the configuration file. For HttpApplication, when it handles different stages of an HTTP request, it triggers different events (event), and HttpModule's meaning is to register HttpApplication's corresponding event, Inject the required operations into the process of the entire HTTP request. Asp. NET many functions, such as authentication, authorization, caching, etc., are implemented by the corresponding HttpModule.

The final completion of the processing of the HTTP request is implemented in another important object: HttpHandler. For different resource types, there are different httphandler. For example, the. aspx page corresponds to the HttpHandler of the. svc file for the SYSTEM.WEB.UI.PAGE,WCF. HttpHandler for System.ServiceModel.Activation.HttpHandler. The entire processing flow above is shown in Figure 7 .

Figure 7 ASP. NET Processing pipeline

HttpApplication

HttpApplication is the core of the entire ASP. NET infrastructure, and it handles the HTTP requests that are distributed to it. Because a HttpApplication object can handle only one request at a time, the HttpApplication can be used for subsequent processing of requests only after the processing of a request has been completed. So, ASP. NET uses the mechanism of object pooling to create or acquire HttpApplication objects. Specifically, when the first request arrives, ASP. NET creates multiple HttpApplication objects at once and places them in the pool, selecting one of the objects to handle the request. When processing is complete, the HttpApplication is not recycled, but is released into the pool. For subsequent requests, the idle HttpApplication object is fetched from the pool if all the HttpApplication objects in the pool are in a busy state, ASP. NET creates a new HttpApplication object.

HttpApplication processing the entire lifecycle of a request is a relatively complex process that triggers the corresponding event at different stages of the process. We can register the corresponding event to inject our processing logic into a phase of the HttpApplication processing request. The HttpModule we are going to introduce is to implement the corresponding function through the mechanism of HttpApplication event registration. Table 1 lists the event names that are triggered by HttpApplication when processing each request, as implemented.

Table 1

Name

Describe

BeginRequest

The BeginRequest event is triggered when the HTTP pipeline starts processing the request

Authenticaterequest,postauthenticaterequest

Asp. NET successively triggers these two events to enable the security module to authenticate the request

Authorizerequest,postauthorizerequest

Asp. NET triggers both events, enabling the security module to authorize the request process

Resolverequestcache,postresolverequestcache

Asp. NET has triggered both events so that the cache module leverages the cached direct process response to the request (the cache module can cache the response content process and, for subsequent requests, directly return the cached content, thereby improving responsiveness).

Postmaprequesthandler

For access to different resource types, ASP. NET has different HttpHandler for its process processing. For each request, ASP. NET chooses to match the corresponding HttpHandler type through the extension, and the implementation is triggered after a successful match

Acquirerequeststate,postacquirerequeststate

Asp. NET successively triggers these two events so that the state management module gets the corresponding state based on the current request, such as sessionstate

Prerequesthandlerexecute,postrequesthandlerexecute

Asp. NET finally through a request resource type corresponds to the HttpHandler implementation of the request processing, before and after the implementation of HttpHandler, these two implementations are triggered successively

Releaserequeststate,postreleaserequeststate

Asp. NET triggers both events so that the state Management module releases the corresponding state based on the current request

Updaterequestcache,postupdaterequestcache

Asp. NET has triggered both events so that the cache module saves the HttpHandler processing request to the output cache

Logrequest,postlogrequest

Asp. NET successively triggers both events for the current request process logging

EndRequest

The endrequest event is triggered when the entire request process is complete

For an ASP. HttpApplication is derived from the Global.asax file, we can customize the request handling behavior of HttpApplication by creating a Global.asax file. Global.asax uses a very straightforward way to implement such a function, which is not the method that we commonly use to rewrite (methods overriding) or event registration, but rather directly with the method name matching. In Global.asax, we follow this method of naming the rules for event registration: Application_{event name}. For example, the Application_BeginRequest method is used to handle HttpApplication beginrequest events. If you create a Global.asax file with VS, the following is the default definition.

   1: <%@ application language= "C #"%>
   2: <script runat= "Server" >
   3:void Application_Start (object sender, EventArgs e) {}
   4:void Application_End (object sender, EventArgs e) {}
   5:void Application_Error (object sender, EventArgs e) {}
   6:void Session_Start (object sender, EventArgs e) {}
   7:void Session_End (object sender, EventArgs e) {}
   8: </script>

HttpModule

Asp. NET provides a powerful platform for creating various. NET Web applications, with a highly scalable engine and the ability to handle requests for different resource types. So, what is the achievement of the high scalability of ASP. HttpModule is a must.

Functionally speaking, HttpModule is like an ISAPI filter for IIS. Before IIS distributes the received requests to the appropriate ISAPI extension, the registered ISAPI filter intercepts the request first. The ISAPI filter can obtain and even modify the requested content to complete some additional functionality. Similarly, when a request is transferred to an ASP. NET pipeline, the final responsibility for processing the request is the HttpHandler object that matches the requested resource type, but before handler is formally working, ASP will load and initialize all configured HttpModule objects first. HttpModule in the process of initialization, some functions are registered to HttpApplication corresponding events, then the corresponding events will be triggered at some stage of HttpApplication the entire request processing life cycle. Event handlers that are registered through HttpModule are also executed.

All HttpModule implements the IHttpModule interface, and the following is the definition of IHttpModule. Where the Init method is used to implement the initialization of HttpModule itself, the method accepts a HttpApplication object, and with this object, event registration is easy.

   1:public Interface IHttpModule
   2: {
   3:      void Dispose ();
   4:     void Init (HttpApplication context);
   5:}

Asp. NET provides many of the basic component (Infrastructure) functionality is implemented through the corresponding HttpModule, the following class lists some typical HttpModule:

    • outputcachemodule: The function of output cache is realized, which is Caching.
    • SessionStateModule: A session-based state is implemented on a stateless HTTP protocol;
    • WindowsAuthenticationModule + formsauthenticationmodule + passportauthentication-module : 3 Typical authentication methods are implemented: Windows Authentication, forms authentication and Passport authentication;
    • UrlAuthorizationModule + FileAuthorizationModule: Implements authorization based on URI and file ACL (Access Control List).

While another important httpmodule is related to WCF, it is system.servicemodel. Activation.httpmodule. HttpModule is defined in the System.ServiceModel assembly and, by default, HttpModule completes the IIS-based boarding work.

In addition to these system-defined HttpModule, we can also customize the Httpmoudle. With Web. config, we can easily register it with our web app.

HttpHandler

If HttpModule is equivalent to an ISAPI filter for IIS, we can say that HttpHandler is equivalent to the role of IIS's ISAPI Extension,httphandler acting as the final processor of the request in ASP. For requests of different resource types, ASP. NET loads different handler to handle, that is, the. aspx page is different from the handler that corresponds to the. asmx Web service.

All HttpHandler are implemented with interface IHttpHandler. The following is the definition of IHttpHandler, and the method ProcessRequest provides the implementation that handles the request.

   1:public interface IHttpHandler
   2: {
   3:     void ProcessRequest (HttpContext context);
   4:     bool isreusable {get;}
   5:}

For some HttpHandler, there is a httphandlerfactory associated with it, which is used to create or obtain the corresponding HttpHandler. Httphandlerfactory implements the interface IHttpHandlerFactory, the method gethandler is used to create a new HttpHandler, or to obtain HttpHandler that already exists.

   1:public interface IHttpHandlerFactory
   2: {
   3:     IHttpHandler GetHandler (HttpContext context, string RequestType, String url, string pathtranslated);
   4:     void Releasehandler (IHttpHandler handler);
   5:}

The types of HttpHandler and Httphandlerfactory can be configured in the same way to Web. config. The following configuration contains HttpHandler configurations for 3 typical resource types:. aspx,.asmx and. Svc. You can see that the WCF service-based HttpHandler type is: System.ServiceModel.Activation.HttpHandler.

   1: <?xml version= "1.0" encoding= "Utf-8"?>
   2: <configuration>
   3: <system.web>
   4: 
   5: <add path= "*.svc" verb= "*" type= "System.ServiceModel.Activation.HttpHandler, System.ServiceModel, version= 3.0.0.0, Culture=neutral, publickeytoken=b77a5c561934e089 "validate=" false "/>
   6: <add path= "*.aspx" verb= "*" type= "System.Web.UI.PageHandlerFactory" validate= "True"/>
   7: <add path= "*.asmx" verb= "*" type= "System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services , version=2.0.0.0, Culture=neutral, publickeytoken=b03f5f7f11d50a3a "validate=" False "/>
   8: 
   9: </system.web>
  Ten: </configuration>

Asp. NET pipeline

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.