"Reprint" IIS and ASP.

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

Read Catalogue

    • What is ASP.
    • HTTP protocol
    • IIS and ASP.
    • ASP. NET Pipeline
    • Resources

We are developing Web programs based on ASP, basically publish to a Windows Server with IIS installed, and then as long as the user is able to access even if the task is complete, but seldom think about what is going on behind it, then this series summarizes the fundamentals of ASP.

Back to Top

What is ASP.

We do web development can say all the moment with ASP. NET, but rarely summed up what is the ASP, can be summed up in one sentence:

ASP. NET is a platform for developing Web applications.

Back to Top

HTTP protocol

Because the Web program is based on the HTTP protocol, it is necessary to learn the HTTP protocol before continuing in-depth understanding of ASP.

The HTTP protocol is an application layer, based on the TCP/IP protocol, used to transfer hypertext from a World Wide Web server to a local browser. The HTTP protocol works on a client/server-side architecture, such as.

The HTTP protocol has the following notable features:

1, simple and fast, when the client initiates a request to the server, it simply sends the request method and URL. Common request methods include: Get,post and so on.

2, flexible, because the HTTP protocol allows the sending of any type of data object, the data type can be specified by Content-type.

3, no connection and no status. No connection means that each connection processes only one request, the server finishes processing the request, and the client disconnects when the request is received. Stateless means that each request is independent.

4, is generally based on the B/s structure.

Back to Top

IIS and ASP.

ASP. NET is inextricably linked to IIS.

1,iis 5.x and ASP .

IIS 5.x is integrated on Windows Server 2000.

First look at the diagram below, which shows how to handle ASP. NET program requests under IIS 5.x.

When an HTTP request is detected, the request is based on the extension to determine whether the requested is a static resource (such as. Html,.img,.txt,.xml, etc.), and if so, returns the contents of the file directly as an HTTP response. If it is a dynamic resource (such as. aspx,asp,php, etc.), the appropriate ISAPI Dll is located from the script Map of IIS with the extension.

ISAPI is an abbreviation for the Internet Server API (Internet Server application Programming Interface) and is a set of local (Native) Win32 APIs. High execution performance is a link between IIS and other dynamic Web applications or platforms. For example, ASP ISAPI bridges IIS and ASP, while the ASP. NET ISAPI connects IIS with ASP. Ispai is defined in a DLL, the corresponding DLL for the ASP. aspnet_isapi.dll, you can "%windir%\microsoft.net\framework\{version" in the directory "no }\ "found in the DLL.

ISAPI supports ISAPI extensions (ISAPI Extension) and ISAPI filtering (ISAPI filter), which is the interface that really handles HTTP requests, which can be viewed, modified, forwarded, or rejected before the HTTP request is actually processed. For example, IIS can take advantage of ISAPI filtering for request validation (authentication).

If we are requesting an ASP. NET-based resource type, such as:. aspx Web Page,. asmx Web service, or. svc WCF service, etc., the aspnet_isapi.dll will be loaded, ASP. The ISAPI extension creates an ASP. NET worker process (if the process has not yet started), and for IIS 5.x the worker process is aspnet.exe. The IIS process communicates with the worker process through a named pipe (Named Pipes) process for best performance.

During the initialization of a worker process, the. NET Runtime (CLR) is loaded, thus building a managed environment. For a web app's initial request, the CLR creates an AppDomain for it. In this AppDomain, the HTTP runtime (HTTP runtime) is loaded and used to create the appropriate application. All Web apps that are hosted on IIS 5.x run in different AppDomain in the same process (worker process aspnet_wp.exe).

2,iis 6.0 and ASP .

IIS 6.0 is integrated on Windows Server 2003.

as you can see from the above, IIS 5.x still has the following two issues:

    • The ISAPI DLL is loaded into the InetInfo.exe process, which is a typical cross-process communication between the worker processes, although with the best-performing named Pipes, it still presents a performance bottleneck.
    • All ASP. Run in the same process (aspnet_ Wp.exe) in different application domains (AppDomain), the isolation level based on the application domain does not fundamentally address the impact of one application on another, and more often than not, we need different Web applications to run in different processes.

So how do you handle ASP. NET requests under IIS 6.0, for example.

In IIS 6.0, in order to solve the first problem, ISAPI.dll is loaded directly into the worker process. In order to solve the 2nd problem, the mechanism of application pools (application pool) was introduced. We can create application pools for one or more Web applications, each with a separate worker process, providing a process-based isolation level for Web apps running in different application pools. The worker process name for IIS 6.0 is w3wp.exe.

Of course, in addition to the above two improvements, IIS 6.0 has some other commendable points, 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 (Kernel mode) and is present as a driver. It is part of the TCP/IP network subsystem of Windows 2003 and, structurally, it belongs to a network driver on top of TCP. Strictly speaking, HTTP. SYS is no longer part of IIS, so http. SYS's configuration information is not stored in the IIS Metabase (Metabase), but is defined in the registry. HTTP. The registry key for Sys is located in the following path: Hkey_local_machine/system/currentcontrolset/services/http. HTTP. SYS can bring the following benefits:

Continuous monitoring: Due to HTTP. SYS is a network driver, always in a running state, for the user's request, can respond in a timely manner;
Better stability: http. SYS runs in operating system kernel mode and does not execute any user code, so it is not inherently affected by web applications, worker processes, and IIS processes;
Kernel-mode data caching: If a resource is requested frequently, HTTP. SYS caches the content of the response, and the cached content responds directly to subsequent requests. Because this is a kernel-based cache, there is no kernel-mode and user-mode switching, and the response speed will be greatly improved.
Figure 2 shows the structure of IIS and the process of handling HTTP requests. As you can see, unlike IIS 5.x, W3SVC is detached from the InetInfo.exe process (for IIS6.0, InetInfo.exe can basically be considered a purely IIS management process) and run in another process SvcHost.exe. However, the basic function of W3SVC has not changed, but has made the corresponding improvement in the realization of the function. As with IIS 5.x, the metabase (Metabase) still exists in the InetInfo.exe process.

When HTTP. sys listens to the user's HTTPS request, it is distributed to W3SVC. W3SVC parses the requested URL and obtains the target application based on the mapping between the URL obtained from metabase and the Web application, and further obtains the application pool or worker process that the target application runs. If the worker process does not exist (it has not been created or reclaimed), a new worker process is created for the request, and this creation of the worker process is called a request-creation. During the initialization of the worker process, the corresponding ISAPI.dll is loaded and the ISAPI.dll loaded is aspnet_ispai.dll for the ASP. The ASP. NET ISAPI is responsible for CLR loading, AppDomain creation, WEB application initialization, and so on.

Therefore, the changes to IIS 5.x,iis 6.0 are mainly in the following areas:

    • The ISAPI.dll is loaded directly into the worker process, which resolves the 1th issue of IIS 5.x.
    • introduced a mechanism for application pools (application pool).
    • Created a new HTTP listener, HTTP protocol stack (HTTP Protocol stack, or. sys)

3,iis 7.0 and ASP .

IIS 7.0 is integrated on Windows Server 2008.

To IIS 7.0, how to handle the ASP.

IIS 7.0 has also revolutionized the listening and distribution mechanisms of requests, mainly in the introduction of Windows Process Activation Services (Activation Service,was), which will be the original (IIS 6.0) W3SVC part of the function that was carried by the Specifically, through the above introduction, we know that for IIS 6.0来 said, W3SVC mainly carries three major functions:

    • HTTP request reception: receives HTTP requests that are heard by the. Sys;
    • Configuration management: Configure the related components by loading configuration information from the metabase (Metabase);
    • Process management: Create, recycle, and monitor worker processes.

In IIS 7.0, the last two sets of features were moved into was, and the task that received the HTTP request still fell on the W3SVC header. was introduced as an unprecedented feature of IIS 7.0: processing both HTTP and non-HTTP requests. In was, an important interface: The Listener Adapter interface (Listener Adapter Interface) abstracted the requests that were heard by different protocol listeners. As for the listener under IIS, in addition to the network-driven HTTP. SYS, which provides 3 types of listeners: TCP Listener, named pipe (Named Pipes) listener, and MSMQ listener, each provides TCP-based, The listener feature for named Pipes and MSMQ transport protocols. In contrast to this 3 listener, 3 listener adapters (Adapter) provide the adapter between the listener and the listener adaptor interface. In this sense, w3svc in IIS 7.0 is more capable of monitoring the adapter for HTTP. sys. These 3 types of listener and listener adapters that WCF provides are defined in assembly SMHost.exe, and you can find the assembly in the following directory:%windir%\microsoft.net\framework\v3.0\windows Communication Foundatio.

These 3 types of listeners and listeners provided by WCF are ultimately reflected in the form of Windows service, although they are defined in an assembly, and we are still starting, terminating, and configuring them individually through the service work manager (Scm,service Control Manager). SMHost.exe provides 4 important Windows Service:

    • Nettcpportsharing: Provides TCP port sharing for WCF, about port sharing;
    • Nettcpactivator: A TCP-based activation request was provided for was, including a TCP listener and a corresponding listener adapter;
    • Netpipeactivator: A named pipe-based activation request was provided for was, including a named pipe listener and a corresponding listener adapter;
    • NetMsmqActivator: An MSMQ-based activation request was provided for was, including the MSMQ Listener and the corresponding listener adapter.

For the above 4 Windows services are rendered in the Service Control Manager (SCM).

The 1th diagram reveals the overall architecture of IIS 7.0 and the entire request processing process. Either the HTTP request received from W3SVC or the request received by the listener adapter provided by WCF 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. Was in the process of request processing, through the built-in configuration management module to load the relevant configuration information to configure the related build, and IIS 5.x and IIS 6.0 based on the metabase configuration Information Store, IIS 7.0 is most of the configuration information in the form of XML configuration files. The basic configuration is stored in the applicationhost.cofig.

Therefore, the changes to IIS 6.0,iis 7.0 are mainly in the following areas:

    • The benefits of introducing the Windows Process Activation service (Windows Processes Activation Service,was) and diverting some of the functionality from the original (IIS 6.0) w3svc to was, HTTP requests and non-HTTP requests can be processed at the same time.
    • Unlike the metabase-based configuration information store for IIS 5.x and IIS 6.0, most IIS 7.0 stores configuration information in the form of an XML configuration file. The basic configuration is stored in the applicationhost.cofig.

Back to Top

ASP. NET Pipeline

Look at two pictures first.

ASP. NET Pipeline

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 the 1th figure.

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.

19 Events for the classic ASP. NET Pipeline:

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.

<%@ 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) {}& Lt;/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.

Public interface IHttpModule {    void Dispose ();    void Init (HttpApplication context); }

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 certification and Passport certification;
    • 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.

Public interface IHttpHandler {    void ProcessRequest (HttpContext context);    BOOL IsReusable {get;}}

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.

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

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<system.web>
<add path= "*.svc" verb= "*" type= "System.ServiceModel.Activation.HttpHandler, System.ServiceModel, version= 3.0.0.0, 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=2.0.0.0, Culture=neutral, publickeytoken=b03f5f7f11d50a3a "validate=" False "/>
</system.web>
</configuration>

Back to top 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

"Turn from" https://www.cnblogs.com/mcgrady/p/7150548.html

"Reprint" IIS and ASP.

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.