Saturday, February 18, 2012

IIS- ASP.NET process model

Here are the basic description about how IIS handles an ASP.NET request.

These are the terms you should know before understanding the process model : 
IIS - IIS stands for Internet information server. It is one of most powerful servers from Microsoft.
Worker process - ASP.NET runs under a process named as worker process. It is responsible for all request and response processing.
Application pool - Application pool is container of worker process(IIS 6). It was not available in IIS5.
HTTP - Hyper test transfer Protocol, an application level protocol which relies on TCP and IP to transmit data between 2 nodes connected on network.
ISAPI - Internet server application programming interface.ISAPI extensions are modules implemented on plain old win32.dll, on which IIS relies to process specific resources.
HTTPRuntime Environment - Where all ASP.NET processing takes place and all .NET managed code is available.
Web Administration service : The Web Administration Service is responsible for configuration and process management. It’s essentially the bridge between the HTTP.SYS file and the application pools. The Web Administration Service’s configuration responsibilities come into play when IIS is initialized. The Web Administration Service uses metabase information to compile a routing table that maps HTTP requests to the associated application pools. The process management responsibilities include starting, stopping, and restarting worker processes as needed by the system.

ASP.NET request in IIS5:




















    When installed ASP.NET configured IIS to redirect requests for ASP.NET specific files to a ISAPI extension called aspnet_isapi.dll. IIS has mapping in its meta base for .extensions and componenet responsible to handle this extension.
     IIS inetinfo.exe process listen to TCP port 80(default) for incoming HTTP request and queue them to process. If the request is for ASP.NET page then processing is delegate to ASP.NET ISAPI dll.  All ASP.Net requests are dispatched by ISAPI extension to an external worker process called as aspnet_wp.exe along with all information related to the request. aspnet_wp.exe is common to all ASP.NET request on the server. The ISAPI communicats with aspnet_wp.exe via NamedPipes. This worker process take care of delivering the request to ASP.NET HTTP runtime environment. As there is only once instance of worker process thus requests for different appliation(virtual dorectory) is separated by different APPDomain.
    The major limitation here is that the single process host all the application, thus if killing this process(because of misbehave of any application) will cause every other application to restart.

ASP.NET Request in IIS6:

















The concept of application pool was introduced in IIS6. Unlike IIS5 single aspnet_wp.exe, in IIS6 there is worker process w3wp.exe, which is not single. It has many instances depending upon configuration. Thus in IIS different application can run in different apppool, which can contain one or many worker process.
     In IIS6 there are 2 level: User and Kernel.
     In IIS6 the request are handled and queued at kernel level by kernel driver called http.sys. Once the request comes to http.sys it redirects it to corresponding application pool queue(user mode). The question is how HTTP.SYS knows about which appPool will handle this request. The answer is whenever new appPool is generated its ID is regitered with HTTP.SYS. So AppPool. These incoming requests are forwarded to AppPool via WAS.
    This module is responsible for reading worker process - web application binding from IIS metabase and forwarding the request to right worker process. Each queue belong to specific appPool and thus processed by specific worker process.
    Here this worker process is responsible for loading ISAPI extension, which in turn loads the CLR and delegate all work to HTTPRuntime.
   w3wp.exe is not ASP.NET specific, it handled other requests also. In case of ASP.NET it will load the ISAPI dll and the process it further.

Both IIS5 and IIS6:
Once the HttpRuntime is loaded then comes the sequence of HttpModule, which intercept the request and executes different handlers. These modules are also called as HttpPipiline, by which every ASP.NET request will be passed.
   Here is the list of handlers that are called:
Event                                           When It's Called
BeginRequest                             Before request processing starts
AuthenticateRequest                  To authenticate client
AuthorizeRequest                       To perform access check
ResolveRequestCache                To get response from cache
AcquireRequestState                  To load session state
PreRequestHandlerExecute       Before request sent to handler
PostRequestHandlerExecute     After request sent to handler
ReleaseRequestState                  To store session state
UpdateRequestCache                 To update response cache
EndRequest                                 After processing ends
PreSendRequestHeaders           Before buffered response headers sent
PreSendRequestContent            Before buffered response body sent