IIS and asp.net pipelines and IISasp.net Pipelines

Source: Internet
Author: User
Tags classic asp what is asp msmq metabase

IIS and asp.net pipelines and IISasp.net Pipelines

We developed web programs based on asp.net, which are basically released and deployed on the windows Server with IIS installed. Once the user can access them, the task is completed, however, I seldom calm down and think about what happened behind this, so this series will summarize the basic principles of asp.net.

What is asp.net? What we do in web development can be said to be dealing with asp.net all the time, but we seldom sum up what asp.net is. We can sum it up in one sentence: Asp.net is a platform for developing web programs.HTTP protocol because web programs are based on HTTP protocol, it is necessary to learn the HTTP protocol before continuing to understand asp.net. HTTP is a transfer protocol that belongs to the application layer and is based on TCP/IP protocol. It is used to transmit hypertext to a local browser from a World Wide Web server. The HTTP protocol works on the client/server architecture, for example.

 

 

HTTP has the following notable features:

1. simple and fast. When a client initiates a request to the server, it only needs to send the request method and url. Common Request methods include GET and POST. 2. Flexible because HTTP allows sending any Type of data objects, the data Type can be specified through Content-Type. 3. No connection or status. No connection means that each connection only processes one request. After the server processes the request and the client receives the request, the connection is closed. Stateless means that each request is independent. 4. Generally, it is based on the B/S architecture. IIS and asp.net are closely related to IIS. 1. IIS 5.x and asp.netIIS 5.x is integrated on windows server 2000. First, let's take a look at the figure below. This figure shows how to process asp.net program requests under IIS 5.x.

When an HTTP requestrequest is detected, the request for an extension name is a static resource (such as .html, .img,.txt,. xml, and so on). If yes, the file content is directly returned in the form of HTTP Response. For dynamic resources (such as. aspx, asp, php, and so on), the corresponding ISAPI Dll is found through the Script Map of IIS through the extension.

ISAPIIs an Internet Server API (Internet Server Application Programming Interface) abbreviation, is a set of local (Native) Win32 API, with high execution performance, is the link between IIS and other dynamic Web applications or platforms. For example, asp isapi bridges IIS and ASP, while ASP. net isapi connects IIS and ASP. NET. ISPAI is defined in a Dll. The Dll corresponding to ASP. net isapi isAspnet_isapi.dllYou can find the Dll in the directory "% windir % \ Microsoft. NET \ Framework \ {version no.

ISAPI supports ISAPI Extension and ISAPI Filter. The former is an interface that truly processes HTTP requests, the latter can view, modify, forward, or reject an HTTP request before it is actually processed. For example, IIS can use ISAPI filtering to Authentication the request ).

If the request is based on ASP. NET resource type, such :. aspx Web Page ,. asmx Web Service or. svc WCF Service, etc. Aspnet_isapi.dll will be loaded, ASP. net isapi extension will create ASP. NET working process (if the process has not been started), for IIS 5.x, the working process is aspnet.exe. The IIS process communicates with the working process through the Named pipeline (Named Pipes) process to achieve the best performance.

During the initialization process, the. NET Runtime (CLR) is loaded to build a hosted environment. The CLR creates an AppDomain for the first request of a Web application. In this AppDomain, HTTP Runtime is loaded and used to create corresponding applications. All Web applications hosted on IIS 5.x are running in different AppDomains in the same process (Working Process aspnet_wp.exe.

  2. IIS 6.0 and asp.netIIS 6.0 is integrated on windows server 2003. As you can see above, IIS 5.x still has the following two problems:
  • ISAPI dllis redirected to the inetinfo.exe process. It is a typical cross-process communication mode between the ISAPI and the working process. Although the best-performing named pipeline is used, it still brings performance bottlenecks.
  • All asp.netapplications run in different AppDomains in the same process (aspnet_wp.exe). The isolation level based on application domains cannot fundamentally solve the impact of one application on another, we need different Web applications to run in different processes.

In IIS 6.0, how does one handle asp.net requests.

 

In IIS 6.0, ISAPI. dll is directly loaded into the working process to solve the first problem. To solve the 2nd problems, the Application Pool mechanism is introduced. We can create an application pool for one or more Web applications. Each application pool corresponds to an independent workflow, this provides process-based isolation levels for Web applications running in different application pools. The IIS 6.0.

Of course, in addition to the above two improvements, there are some other commendable aspects of IIS 6.0, the most important of which is the creation of a new HTTP listener: HTTP Protocol Stack (HTTP Protocol Stack, HTTP. SYS ). HTTP. SYS runs in Windows Kernel Mode and exists as a driver. It is part of the TCP/IP network subsystem of Windows 2003. In terms of structure, it is a network driver over TCP. Strictly speaking, HTTP. SYS is no longer in the scope of IIS. Therefore, the configuration information of HTTP. SYS is not stored in the Metabase of IIS, but defined in the registry. The Registry Key of HTTP. SYS is located in the following path: HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/HTTP. HTTP. SYS can bring the following benefits:

Continuous listening: Because HTTP. SYS is a network driver and is always running, it can respond to users' HTTP requests in a timely manner;
Better stability: HTTP. SYS runs in the operating system kernel mode and does not execute any user code. Therefore, HTTP. SYS is not affected by Web applications, working processes, and IIS processes;
Data cache in kernel mode: If a resource is frequently requested, HTTP. SYS caches the response content, and the cached content can directly respond to subsequent requests. Because this is a cache based on the kernel mode, there is no switch between the kernel mode and the user mode, and the response speed will be greatly improved.
Figure 2 shows the structure of IIS and the process for processing HTTP requests. It can be seen that it is in IIS environment. However, the basic functions of W3SVC have not changed, but are improved accordingly. In the same way as IIS 5.x, the metadata Library (metabaseworkflow) still exists in the inetinfo.exe process.

When HTTP. SYS listens to a user's HTTP request, it distributes it to W3SVC. W3SVC parses the request URL, obtains the target application based on the ing relationship between the URL obtained from Metabase and the Web application, and obtains the application pool or worker process that the target application runs. If a workflow does not exist (not created or recycled), a new workflow is created for the request. This method of creation is called a request-type creation. During the initialization of the working process, the corresponding ISAPI. dll is loaded. for ASP. NET applications, the ISAPI. dll to be loaded is aspnet_isw..dll. ASP. net isapi is responsible for CLR loading, AppDomain creation, and Web Application initialization.

Therefore, compared with IIS 5.x, IIS 6.0 has the following changes:

  • ISAPI. dll is directly loaded into the working process, which solves 1st problems of IIS 5.x.
  • The Application Pool mechanism is introduced.
  • A new HTTP listener (HTTP Protocol Stack, namely HTTP. sys) is created)
  3. IIS 7.0 and asp.netIIS 7.0 is integrated on windows server 2008. In IIS 7.0, how does one handle asp.net requests.

 

IIS 7.0 has also improved the request listening and distribution mechanism, mainly because of the introduction of Windows Process Activation Service (WAS, some functions carried by the original (IIS 6.0) W3SVC are distributed to WAS. Specifically, through the above introduction, we know that for IIS 6.0, W3SVC mainly carries three main functions:

  • HTTP request receiving: receives the HTTP request listened by HTTP. sys;
  • Configuration Management: load configuration information from Metabase to configure related components;
  • Process Management: creates, recycles, and monitors work processes.

In IIS 7.0, the last two groups of functions are moved into WAS, and the tasks for receiving HTTP requests are still on the W3SVC header. WAS is an unprecedented feature of IIS 7.0: simultaneously processing HTTP and non-HTTP requests. In WAS, a Listener Adapter Interface (Listener Adapter Interface) is used to abstract the requests listened to by different protocol listeners. For listeners under IIS, except for network-driven HTTP. in addition to HTTP request listening, SYS provides three types of listeners: TCP listener, Named Pipes listener, and MSMQ listener, TCP, named pipe, and MSMQ transmission protocols are provided. Compared with the three listeners, the three listener adapters provide adaptation between listeners and listener Adapter interfaces. In this sense, W3SVC in IIS 7.0 serves more as the listener adapter for HTTP. SYS. The three invigilators and invigilators of wcf.pdf are defined in the program set smhost.exe. You can find the assembly in the following directory: % windir % \ Microsoft. NET \ Framework \ v3.0 \ Windows Communication Foundatio.

These three listeners and listening adapters provided by WCF are eventually reflected in the form of Windows Service. Although they are defined in a program set, we still use the Service work Manager (SCM, Service Control Manager) start, terminate, and configure the instance separately. SMHost.exe provides four important Windows Services:

  • NetTcpPortSharing: Provides TCP port sharing for WCF;
  • NetTcpActivator: Provides TCP-based activation requests for WAS, including TCP listeners and corresponding listening adapters;
  • NetPipeActivator: provides a named pipeline-based activation request for WAS, including the named pipeline listener and the corresponding listening adapter;
  • NetMsmqActivator: Provides MSMQ-based activation requests for WAS, including MSMQ listeners and corresponding listening adapters.

Displays the preceding four Windows Services in the Service Control Manager (SCM.

 

The 1st images reveal the overall structure of IIS 7.0 and the entire request processing process. Whether it is an HTTP request received from W3SVC or a request received through the listener adapter provided by WCF, it will eventually be passed to WAS. If the corresponding Worker Process (or application pool) has not been created, it is created; otherwise, the request is distributed to the corresponding Worker Process for subsequent processing. During request processing, WAS loads the relevant configuration information through the built-in configuration management module to configure the relevant components, and IIS 5. different from the Metabase-based configuration information storage in IIS 6.0, most of the configuration information in IIS 7.0 is stored in XML configuration files. The basic configuration is stored in applicationHost. cofig.

Therefore, compared with IIS 6.0, IIS 7.0 changes mainly in the following aspects:

  • The Windows Process Activation Service (WAS) is introduced to distribute some functions of the original (IIS 6.0) W3SVC to WAS, it can process both HTTP requests and non-HTTP requests.
  • Unlike the Metabase-based storage of configuration information in IIS 5.x and IIS 6.0, most of the configuration information in IIS 7.0 is stored in XML configuration files. The basic configuration is stored in applicationHost. cofig. 
The asp.net pipeline first looks at two images.

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 process above is shown in Figure 1st.

 

HttpApplication

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.

19 events in the classic asp.net pipeline:

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

PostMapRequestHandler

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

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

 

HttpModule

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

 

HttpHandler

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 IHttpHandler2 {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 IHttpHandlerFactory2 {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> 4     

 

 

References
  • Http://www.cnblogs.com/artech/archive/2009/06/20/1507165.html
  • Http://www.cnblogs.com/OceanEyes/archive/2012/08/13/aspnetEssential-1.html
  • Http://www.cnblogs.com/wenthink/archive/2013/05/06/HTTP_IIS_ASPNET_Pipeline.html

 

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