ASP. net pipe events and HttpModule, HttpHandler simple understanding,

Source: Internet
Author: User

ASP. net pipe events and HttpModule, HttpHandler simple understanding,

Part 1: Reprinted from Artech IIS and ASP. NET Pipelines


ASP. NET Pipeline

Take IIS as an example. In w3wp.exe, use Aspnet_ispai.dll to load. NET Runtime (if. NET is not loaded at runtime ). IIS 6 introduces the application pool concept. A working process corresponds to an application pool. An application pool can host one or more Web applications, and each Web application maps to an IIS virtual directory. Like IIS 5.x, each Web application runs in its own application domain.

If HTTP. the HTTP request received by SYS is the first access to the Web application. When the runtime is successfully loaded, it creates an application domain (AppDomain) for the Web application through AppDomainFactory ). Then, a special runtime IsapiRuntime is loaded. IsapiRuntime is defined in the assembly System. Web, and the corresponding namespace is System. Web. Hosting. IsapiRuntime takes over the HTTP request.

IsapiRuntime will first create an IsapiWorkerRequest object to encapsulate the current HTTP request and pass the IsapiWorkerRequest object to ASP. NET runtime: HttpRuntime. From now on, the HTTP request has officially entered ASP.. NET pipeline. According to the IsapiWorkerRequest object, HttpRuntime will create a Context object used to represent the current HTTP Request: HttpContext.

With the successful creation of HttpContext, HttpRuntime will use HttpApplicationFactory to create a new or obtain an existing HttpApplication object. In fact, ASP. NET maintains an HttpApplication Object pool. HttpApplicationFactory selects available HttpApplication users from the pool to process HTTP requests. After processing, it is released to the object pool. HttpApplicationFactory is responsible for processing the current HTTP request.

During the HttpApplication initialization process, the corresponding HttpModule object is loaded and initialized according to the configuration file. For HttpApplication, different events are triggered at different stages of HTTP request processing. The significance of HttpModule is to register the corresponding events of HttpApplication, inject the required operations into the processing process of the entire HTTP request. Many functions of ASP. NET, such as identity authentication, authorization, and caching, are implemented through the corresponding HttpModule.

The final implementation of HTTP request processing is another important object: HttpHandler. Different resource types have different HttpHandler types. For example, the HttpHandler corresponding to the. aspx Page is System. Web. UI. Page, and the HttpHandler corresponding to the. svc file of WCF is System. ServiceModel. Activation. HttpHandler. The entire process above is 7.



Figure 7 ASP. NET MPs queue


HttpApplication is the core of the entire ASP. NET infrastructure and is responsible for processing HTTP requests distributed to it. Because an HttpApplication object can only process one request at a time, HttpApplication can be used for subsequent request processing only after a request is processed. Therefore, ASP. NET uses the object pool mechanism to create or obtain an HttpApplication object. Specifically, when the first request arrives, ASP. NET creates multiple HttpApplication objects at a time, puts them in the pool, and selects an object to process the request. When the processing is completed, the HttpApplication will not be recycled, but will be released to the pool. For subsequent requests, idle HttpApplication objects are retrieved from the pool. If all the HttpApplication objects in the pool are busy, ASP. NET creates a new HttpApplication object.

The entire lifecycle of requests processed by HttpApplication is a relatively complex process. Events are triggered at different stages of the process. We can register corresponding events and inject our processing logic into a stage of HttpApplication processing requests. The HttpModule that we will introduce next implements relevant functions through the HttpApplication event registration mechanism. Table 1 lists the names of events triggered by HttpApplication when processing each request according to implementation.

Table 1




The BeginRequest event is triggered when the HTTP pipeline starts processing requests.

AuthenticateRequest, PostAuthenticateRequest

ASP. NET has successively triggered these two events, enabling the Security Module to authenticate the request.

AuthorizeRequest, PostAuthorizeRequest

ASP. NET has successively triggered these two events, enabling the Security Module to authorize the request process.

ResolveRequestCache, PostResolveRequestCache

ASP. the cache module uses the cache to directly respond to the request directly (the cache module can cache the response content process for subsequent requests, directly return the cached content to improve the response capability ).


ASP. NET has different HttpHandler processes for accessing different resource types. For each request, ASP. NET selects the corresponding HttpHandler type through the extension. After successful matching, this implementation is triggered.

AcquireRequestState, PostAcquireRequestState

ASP. NET triggers these two events successively, so that the status management module obtains the corresponding status based on the current request, such as SessionState.

PreRequestHandlerExecute, PostRequestHandlerExecute

ASP. NET finally processes requests through HttpHandler corresponding to the request resource type. Before and after implementing HttpHandler, these two implementations are successively triggered.

ReleaseRequestState, PostReleaseRequestState

ASP. NET triggers these two events successively, so that the Status Management Module releases the corresponding status based on the current request.

UpdateRequestCache, PostUpdateRequestCache

ASP. NET has successively triggered these two events so that the cache module saves the HttpHandler requests to the output cache.

LogRequest, PostLogRequest

ASP. NET successively triggers these two events as logs of the current request process


After the entire request is processed, the EndRequest event is triggered.


For an ASP. NET application, HttpApplication is derived from the global. asax file. You can create a global. asax file to customize the HTTP application request processing behavior. Global. asax implements this function in a very direct way, instead of using Method Overriding or event registration, it directly uses Method name matching. In global. asax, we follow the naming rules for Event registration: Application _ {Event Name }. For example, the Application_BeginRequest method is used to process the BeginRequest event of HttpApplication. If you create a global. asax file through 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>



ASP. NET provides a powerful platform for creating various. NET Web applications. It has a highly scalable engine and can process requests of different resource types. So what makes ASP. NET highly scalable? HttpModule.

Functionally, HttpModule is equivalent to ISAPI Filter in IIS in ASP. NET. Before IIS delivers the received request to the corresponding ISAPI Extension, the registered ISAPI Filter intercepts the request first. The ISAPI Filter can obtain or even modify the request content to complete some additional functions. Similarly, when a request is transferred to ASP. after the. NET pipeline, the final HttpHandler object that is responsible for processing the request matches the request resource type. However, before Handler's official work, ASP. NET will first load and initialize all the configured HttpModule objects. During the initialization process, the HttpModule registers some functions to the corresponding event of the HttpApplication. In this case, the corresponding event will be triggered at a certain stage in the entire request processing lifecycle of the HttpApplication, event Handlers registered with HttpModule can also be executed.

All httpmodules implement the IHttpModule interface. The following is the definition of IHttpModule. The Init method is used to initialize the HttpModule itself. This method accepts an HttpApplication object. With this object, event registration is easy.

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

Many of the basic components (Infrastructure) provided by ASP. NET are implemented through the corresponding HttpModule. The following Classes list some typical httpmodules:

  • OutputCacheModule: implements the Output cache function;
  • SessionStateModule: Implements Session-based status on stateless HTTP protocol;
  • WindowsAuthenticationModule + FormsAuthenticationModule + PassportAuthentication-Module: implements three typical authentication methods: Windows authentication, Forms authentication, and Passport authentication;
  • UrlAuthorizationModule + FileAuthorizationModule: Implements Uri-based and Access Control List authorization.

Another important HttpModule is related to WCF, namely System. ServiceModel. Activation. HttpModule. HttpModule is defined in the System. ServiceModel set. By default, HttpModule completes IIS-based boarding.

In addition to the HttpModule defined by these systems, we can also customize HttpMoudle. Through Web. config, we can easily register it into our Web application.


If HttpModule is equivalent to ISAPI Filter of IIS, we can say that HttpHandler is equivalent to ISAPI Extension of IIS. HttpHandler plays the final handler role of the request in ASP. NET. For requests of different resource types, ASP. NET loads different Handler for processing, that is, the Handler corresponding to. aspx page and. asmx web service is different.

All HttpHandler implement the IHttpHandler interface. The following is the definition of IHttpHandler. The method ProcessRequest provides the implementation of request processing.

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


For some HttpHandler, there is an associated HttpHandlerFactory, which is used to create or obtain the corresponding HttpHandler. HttpHandlerFactory implements the IHttpHandlerFactory interface. The GetHandler method is used to create a new HttpHandler or obtain an existing HttpHandler.

   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 to Web. config in the same way. The following configuration section contains HttpHandler configurations for three typical resource types:. aspx,. asmx, And. svc. You can see that the HttpHandler type based on the WCF Service is System. ServiceModel. Activation. HttpHandler.

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <configuration>
   3: <system.web>
   5: <add path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=, 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=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="False"/>
   9: </system.web>
  10: </configuration>


Part 2:

Related Article

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: 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.