As an ASP. Developer, it is important to understand how IIS and ASP. NET are combined to execute our managed code, as well as the ASP. NET Pipeline event.
This program records
- IIS 5.X
- IIS 6
- IIS 7+
- Integrated mode
- ASP. NET Pipeline
- HttpModule
- Httphandle
IIS 5.x
InetInfo.exe and W3SVC Services
IIS 5.x runs in process InetInfo.exe, and one of the most important services in the process is the Windows service named World Wide Web Publishing Service (W3SVC).
The main features of W3SVC include the interception of HTTP requests, the management of worker processes, and configuration management (by loading related configuration information from metabase).
Request resources (differentiate between static and dynamic resources based on extension)
A static file that returns the contents of the file directly.
Dynamic resource, locate the appropriate ISAPI Dll from the script Map of IIS using the extension name.
Isapi
ISAPI is an abbreviation for the Internet Server API (Internet Server application Programming Interface). Is the link between IIS and other applications.
ISAPI includes ISAPI extension and ISAPI Filter
ISAPI Extension
There will be different ISAPI extensions without the kind of dynamic resources.
such as ASP. NET (. aspx. asmx. SVC, etc.) is aspnet_isapi.dll. Locate the DLL in the directory "%windir%\microsoft.net\framework\{version no}\".
ISAPI Filter
Filter can view, modify, forward, or reject requests before the HTTP request is actually processed, such as IIS can take advantage of ISAPI filtering for request validation (authentication).
Request ASP
If we are asking for an ASP. NET-based resource:
- Load Aspnet_isapi.dll
- Create worker process (first request)
- Load CLR (first request)
- Create an AppDomain (first request for a web app)
- Execute isapiruntime.
Description
- For IIS 5.x, the worker process is aspnet_wp.exe.
- Aspnet_isapi.dll communicates with the worker process through a named pipe (Named Pipes) process for best performance.
- All Web apps that are hosted on IIS 5.x run in different AppDomain in the same worker process.
IIS 6
IIS5 of the poor
- Aspnet_isapi is a cross-process communication with the worker process.
- All Web applications are in the same worker process.
IIS6 Solutions
- Loads the Aspnet_ispai.dll into the worker process.
- Create an application pool that corresponds to a worker process.
Also in IIS6, create a new HTTP listener: HTTP protocol stack (HTTP Protocol stack,http. SYS)
- 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 affected by Web applications, worker processes, and IIS processes in itself;
- 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.
Request ASP
Unlike iis5.x, the following are:
1.W3SVC service creates worker processes on request
2.aspnet_isapi.dll is loaded during the initialization of the worker process.
Description
- The W3SVC service has been detached from the IIS process. HTTP. SYS receives the request and will distribute it directly to the W3SVC service
- In IIS6, the worker process is named W3wp.exe
- This way of creating a worker process is called a request-creation
IIS 7+
W3SVC service
Features of the W3SVC service in IIS6
- HTTP request receive: 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 IIS7, W3SVC is only responsible for the first function, and the remaining function is assigned to was service management
was service
IIS7 introduces the Windows Process Activation service (Windows Processes Activation Service,was): handles both HTTP and non-HTTP requests.
In was, an important interface was defined: The Listener Adapter interface (Listener Adapter Interface) abstracted requests that were heard by different protocol listeners.
Was will listen for HTTP requests for the W3SVC service as well as TCP, Named Pipes, MSMQ3 requests for WCF services.
Description
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 Foundation.
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.
Integrated mode
Disadvantages of the traditional model
- duplicate execution of the same operation: There are some repetitive operations between IIS and ASP, such as authentication;
- inconsistency between dynamic files and static file processing: because only HTTP requests for dynamic files based on ASP. NET (such as. aspx,. asmx,. svc, and so on) can be The ISAPI enters the ASP., and for some static files (such as. html,. xml,. img, and so on) are directly responded to by IIS, Some of the features in the ASP. NET pipeline will not be available for these static file-based requests, for example, we want to apply forms authentication to image-based file requests;
- IIS hard to scale: the extension to IIS is basically a custom ISAPI, but for most people it's not an easy thing to do. Because ISAPI is an unmanaged API based on Win32, it is not an application-oriented programming interface. Often we want to extend IIS in managed code just like HttpModule and HttpHandler, which define ASP.
Integrated mode
In fact, the IIS7 integration mode allows the user to parse the managed code into the IIS kernel code by writing the module of managed code, so that you can precisely control any request for better extensibility.
(each static file in this case also passes through the life cycle event, and the execution efficiency will certainly decrease.)
Differences on the configuration file
<!--traditional mode-- <system.web> <customerrors mode= "RemoteOnly" > <error statuscode= " 404 "redirect=" 404.html "/> <error statuscode=" redirect= "" erro.html "/> </customErrors> <compilation debug= "true" targetframework= "4.5"/> ASP. NET Pipeline
- Load CLR: In a worker process, the ISAPI is responsible for loading the CLR (if the. NET run fashion is not loaded).
- Create AppDomain: When the runtime is successfully loaded, an application domain (AppDomain) is created for the Web app through Appdomainfactory.
- Execute isapiruntime int ProcessRequest (IntPtr ECB, int iwrtype) method
- Pr method for executing httpruntime
- Get HttpApplication instance, call HttpApplication's PR method
- Triggers the HttpApplication events.
Extended
- 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.
- 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.
HttpModule
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.
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.
When the request is transferred to the ASP. NET pipeline, the final responsibility for processing the request is the HttpHandler object that matches the requested resource type, but before handler formally works, 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 implement the IHttpModule interface.
Namespace system.web{public interface IHttpModule { void Init (HttpApplication context); void Dispose ();} }
System-defined 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: 3 Kinds of typical authentication methods are realized: Windows authentication, forms authentication;
- wcfmodule: Enable ASP. net extension out of WCF service (System.ServiceModel.Activation.HttpModule)
Custom Httpmoudle
- Implement Ihttpmoudle
- Modify the configuration file Web. config
Httphandle
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.
Public interface IHttpHandler { bool isreusable {get;} void ProcessRequest (HttpContext context); }
System-defined Httphandle
WebForm aspx file: System.Web.UI.Page
svc file for WCF: System.ServiceModel.Activation.HttpHandler
Mvc:mvchandle
Custom Httphandle
Since handle is mapped to the specified handle before and after Postmaprequesthandler, the Postresolverequestcache
So we can register our handle in the Postresolverequestcache event.
Our Handle.pr method will be called after the PreRequestHandlerExecute.
Extended
HttpApplication has 19 main events, all of which can be accessed via my site's arbitrary address + parameters.
+?pipe can view these event trigger times. For example: Http://neverc.cn?pipe
+?pe can be attached to my website content: Http://neverc.cn?pe
Guess how to achieve the above effect
This address: http://neverc.cnblogs.com/p/4807836.html
This article references: http://www.cnblogs.com/artech/archive/2009/06/20/1507165.html
[ASP.] Talk about IIS and ASP.