HTTP request and Response Process-MPs queue

Source: Internet
Author: User

Take IIS as an example. In w3wp.exe, use aspnet_isapi.dll to load. net Runtime (if. net Running fashion is not loaded), IIS 6.0 introduces the concept of application pool, 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. After the runtime is loaded successfully, it creates an application domain for the Web application through appdomainfactory, then a special runtime isapiruntime is loaded. Isapiruntime is defined in the assembly system. Web. The corresponding namespace is system. Web. Hosting. The loaded 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 the httpruntime at ASP. NET runtime. Since then, the HTTP request has officially entered the ASP. NET pipeline. Httpruntime creates the context object httpcontext that represents the current HTTP request based on the isapiworkerrequest object.

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

Finally, the HTTP request is processed in 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 shown in 1-11.



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. net uses the object pool mechanism to create or obtain the httpapplication object.

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. After processing, httpapplication is released to the pool instead of being recycled. 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 the processing logic into a stage of httpapplication processing requests. Table 1-1 lists the names of events triggered by httpapplication when processing each request in sequence of implementation.

Table 1-1 httpapplication event list


Name Description
Beginrequest 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.. Net triggers these two events successively so that the cache module can directly respond to the request using the cached content (the cache module can cache the response content for subsequent requests, directly return the cached content to improve the response capability)
Postmaprequesthandler ASP. NET has different httphandler processes 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 can save the content obtained by httphandler's request processing to the output cache.
Logrequest, postlogrequest ASP. NET successively triggers these two events to record the current request
Endrequest 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 common methods to override or register events, but directly using method name matching. In global. asax, we perform event registration according to the naming rules such as "application _ {event name. For example, the application_beginrequest method is used to process the beginrequest event of httpapplication. If you use vs to create a global. asax file, the following default definition is used.
<% @ Application language = "C #" %>
<SCRIPT runat = "server">
Void application_start (Object sender, eventargs e ){}
Void application_end (Object sender, eventargs e ){}
Void application_error (Object sender, eventargs e ){}
Void session_start (Object sender, eventargs e ){}
Void session_end (Object sender, eventargs e ){}

ASP. NET has a highly scalable engine that can process requests of different resource types. What makes ASP. NET highly scalable? Httpmodule.

When the request is transferred to ASP. net pipeline, the final request is responsible for processing the httphandler object that matches the request resource type, but before handler officially works, 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. During a certain stage of the HTTP application request processing lifecycle, the corresponding event will be triggered, event Handlers registered with httpmodule can also be executed.

All httpmodules implement a system with the following definitions. web. ihttpmodule interface, where the init method is used to implement the initialization of the httpmodule itself. This method accepts an httpapplication object. With this object, event registration is easy.
Public interface ihttpmodule
Void dispose ();
Void Init (httpapplication context );

Many basic functions provided by ASP. NET are implemented through the corresponding httpmodule. Some typical httpmodules are listed below.

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, forms, and passport.

Urlauthorizationmodule + fileauthorizationmodule: Implements URI-based and Access Control List authorization.

In addition to the httpmodule defined by these systems, we can also customize the httpmodule, which can be easily registered to Web applications through web. config.


For requests of different resource types, ASP. NET loads different handler for processing, that is, the handler corresponding to the. ASPX page and. asmx Web Service is different. All httphandler implement the system. Web. ihttphandler interface with the following definition. The processrequest method provides the implementation of request processing.
Public interface ihttphandler
Void processrequest (httpcontext context );
Bool isreusable {Get ;}

Some httphandler have a related httphandlerfactory, which implements the system. Web. ihttphandlerfactory interface with the following definitions. The gethandler method is used to create a new httphandler or obtain an existing httphandler.
Public interface ihttphandlerfactory
Ihttphandler gethandler (httpcontext context, string requesttype,
String URL, string pathtranslated );
Void releasehandler (ihttphandler handler );

The types of httphandler and httphandlerfactory can be configured to Web. config in the same way. The following section configures httphandler for the. aspx,. asmx, And. SVC resource types.
<System. Web>
<Add Path = "*. SVC"
Verb = "*"
Type = "system. servicemodel. Activation. httphandler,
System. servicemodel, version =, culture = neutral,
Publickeytoken = b77a5c561934e089"
Validate = "false"/>
<Add Path = "*. aspx"
Verb = "*"
Type = "system. Web. UI. pagehandlerfactory"
Validate = "true"/>
<Add Path = "*. asmx"
Verb = "*"
Type = "system. Web. Services. Protocols. webservicehandlerfactory,
System. Web. Services, version =, culture = neutral,
Publickeytoken = b03f5f7f11d50a3a"
Validate = "false"/>
</System. Web>

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.