Learn about ASP. NET MVC5 framework secrets-IIS/ASP. NET pipelines (III), mvc5-iis
ASP. NET Pipeline
Taking iis6.0as an example, the worker will use aspnet_isapi.dll to load the. NET runtime in the w3wp.exe process (if the. NET Runtime is not loaded ). IIS6.0 introduces the application process pool concept. A worker process corresponds to an application pool. An application process pool can host one or more Web applications, and each Web application is mapped to an IIS virtual directory. Like IIS5.x, each Web application runs in its own application domain.
If the HTTP request received by HTTP. SYS is the first access to the Web application, IIS will create an application domain for the Web application through AppDomainFactory after the running is loaded successfully. Then a special runtime IsapiRuntime is loaded. IsapiRuntime is defined in the assembly System. Web. dll. The corresponding namespace is "System. Web. Hosting". The loaded IsapiRuntime takes over the HTTP request.
The IsapiRuntime that takes over the HTTP request will first create an IsapiWorkerRequest object to encapsulate the current HTTP request, and then pass this 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 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. HttpApplication processes the current HTTP request.
During the HttpApplication initialization process, ASP. NET loads and initializes the registered HttpModule object based on 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 are implemented through the corresponding HttpModule.
Finally, HTTP requests are processed. In HttpHandler, different resource types correspond to different types of HttpHandler.
1. HttpApplication
HttpApplication is the core of the entire ASP. NET infrastructure and is responsible for processing the HTTP requests distributed to him. Because an HttpApplication object can only process one request at a time and can only be used for subsequent request processing after processing a request is completed, ASP. 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, the HttpApplication object 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 will create a new HttpApplication object. Otherwise, put the request into the queue and wait for the release of the existing HttpApplication.
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. The following table lists the names of events triggered by HttpApplication when processing each request.
HttpApplication event list
Name |
Description |
BeginRequest |
The BeginRequest event is triggered when the HTTP pipeline starts processing requests. |
AuthenticateRuest, 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, allowing the Security Module to authorize requests. |
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 the matching is successful, this event 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, PostPreRequestHandlerExecute |
ASP. NET finally processes the request through HttpHandler corresponding to the request resource. Before and after implementing HttpHandler, these two events are triggered successively. |
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 follow the naming rules such as "Application _ {Event Name}" to register events. For example, the Application_BeginRequestf method handles the BeginRequest Event of HttpApplication.
2. HttpModule
ASP. NET has a highly scalable engine that can process requests of different resource types. 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 ASP. NET will first load and initialize all the configured HttpModule objects. During the initialization process, the HttpModule registers some callback operations to the corresponding event of the HttpApplication. During the processing of a stage in the lifecycle of the HttpApplication request, the corresponding event will be triggered, event Handlers registered with HttpModule can also be executed.
All httpmodules implement the System. Web. IHttpModule interface with the following definitions. The Init method implements initialization for itself. This method receives an HttpApplication object. With this object, event registration is easy.
public interface IHttpModule{void Dispose();void Init(HttpApplication context);}
ASP. NET provides many basic functions through the corresponding HttpModule implementation, below lists some typical HttpModule, in addition to these system-defined HttpModule, we can also customize the HttpModule, through the Web. config can easily register it to a Web application.
(1) OutputCacheModule: implements the output cache function.
(2) SessionStateModule: Implements session-based state persistence on stateless HTTP protocol.
(3) WindowAuthenticationModule + FormsAuthenticationModule + PassportAuthenticationModule: implements three typical authentication methods: Window, Forms, and Passport.
(4) UrlAuthorizationModule + FileAuthorizationModule: Implements URI-and File ACL-based authorization.
3. HttpHandler
For requests of different resource types, ASP. NET loads different Handler for processing. All HttpHandler implements the System. Web. IHttpHandler interface with the following definitions. The ProcessRequest method defines the implementation of request processing. In another asynchronous version, the IHttpAsyncHandler interface of HttpHandler inherits from IHttpHandler. It processes Requests asynchronously by calling the BeginProcessRequest/EndProcessRequest method.
public interface IHttpHandler{void ProcessRequest(HttpContext context);bool IsReusable{get;}}public interface IHttpAsyncHandler : IHttpHandler{IAsyncRequest BeginProcessRequest(HttpContext context,AsyncCallback ch,object extraData);void EndProcessRequest(IAsyncRequest result);}
Some HttpHandler have a related HttpHandlerFactory. The GetHandler method is defined 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 are configured in Web. config.
In addition to setting up the ing between the HttpHandler type and the request path through configuration, we can also call the RemapHandler method with the following definitions in the current HttpContext to map an HttpHandler object to the current HTTP request. If the HttpHandler display ing is not performed by calling this method, or the input parameter is Null when this method is called, the real HttpHandler object ing occurs before the PostMapRequestHandler of HttpApplication is triggered, by default, HttpHandler is implemented based on Web. config.
public sealed class HttpContext{public void RemapHandler(IHttpHandler handler)}
In other words, when calling the RemapHandler method of the current HttpContext, a specific HttpHandler object is specified to allow ASP. NET to directly skip the default HttpHandler ing operation. Because the default HttpHandler ing occurs before the PostMapRequestHandler event of HttpApplication is triggered, it is meaningful to call the RemapHandler method before this.