Tuesday 29 March 2011

How IIS Process ASP.Net Request

Microsoft Active Server Pages, also known as ASP, since its first release in late 1996 provided web developers with a rich and complex framework for building web applications. As years passed its infrastructure evolved and improved so much that what is now known as ASP.NET is no longer something which resembles its predecessor. ASP.NET is a framework for building web applications, that is, applications that run over the web, where the client-server paradigm is represented mostly by a browser forwarding requests for resources of different kinds to a web server.
The interaction between client and server is very simple. Communications over the web occur via HTTP (Hyper Text Transfer Protocol), an application level protocol which relies on TCP and IP to transmit data between two nodes connected to the heterogeneous network known as World Wide Web.

What is Web Server?

When we want to host the application on a centralized location and wanted to access from many locations. Web server is responsible for handle all the requests that are coming from clients, process them and provide the responses.


What is IIS ?

IIS (Internet Information Server) is one of the most powerful web servers from Microsoft that is used to host your ASP.NET Web application. IIS has it’s own ASP.NET Process Engine  to handle the ASP.NET request. So, when a request comes from client to server, IIS takes that request and  process it and send response back to clients.

Request Processing :

Now let’s have a look how web server & IIS do things internally. Before we move ahead, we have to know about two main concepts
1.    Worker Process
2.   Application Pool
Worker Process:  Worker Process (w3wp.exe) runs the ASP.Net application in IIS. This process is responsible to manage all the request and response that are coming from client system.  All the ASP.Net functionality runs under the scope of worker process.  When a request comes to the server from a client worker process is responsible to generate the request and response. In a single word we can say worker process is the heart of ASP.NET Web Application which runs on IIS.

Application Pool: Application pool is the container of worker process.  Application pools is used to separate sets of IIS worker processes that share the same configuration.  Application pools enables a better security, reliability, and availability for any web application.  The worker process serves as the process boundary that separates each application pool so that when one worker process or application is having an issue or recycles, other applications or worker processes are not affected. This makes sure that a particular web application doesn’t not impact other web application as they are configured into different application pools.
Application Pool with multiple worker process is called “Web Garden”.
Now let’s have look how IIS process the request when a new request comes up from client.
If we look into the IIS 6.0 Architecture, we can divided them into Two Layer
1.    Kernel Mode
2.    User Mode
Now, Kernel mode is introduced with IIS 6.0, which contains the HTTP.SYS.  So whenever a request comes from Client to Server, it will hit HTTP.SYS First.

Now, HTTP.SYS is Responsible for pass the request to particular Application pool. Whenever we creates a new Application Pool, the ID of the Application Pool is being generated and it’s registered with the HTTP.SYS. So whenever HTTP.SYS Receives the request from any web application, it checks for the Application Pool and based on the application pool it send the request. 
So, this was the first steps of IIS Request Processing.
Till now, Client Requested for some information and request came to the Kernel level of IIS means at HTTP.SYS. HTTP.SYS has been identified the name of the application pool where to send. Now, let’s see how this request moves from HTTP.SYS to Application Pool. 

In User Level of IIS, we have Web Admin Services (WAS) which takes the request from HTTP.SYS and pass it to the respective application pool.
When Application pool receive the request, it simply pass the request to worker process (w3wp.exe) . The worker process “w3wp.exe” looks up the URL of the request in order to load the correct ISAPI extension. ISAPI extensions are the IIS way to handle requests for different resources. Once ASP.NET is installed, it installs its own ISAPI extension (aspnet_isapi.dll) and adds the mapping into IIS.  

When Worker process loads the aspnet_isapi.dll, it start an HTTPR untime, which is the entry point of an application. HTTPR untime is a class which calls the Process Request method to start Processing.
When this methods called, a new instance of HTTP Context is been created.  Which is accessible using HTTP Context? Current Properties. This object still remains alive during life time of object request.  Using Http Context. Current we can access some other objects like Request, Response, Session etc.
After that Http Runtime load an Http Application object with the help of  Http Application Factory class.. Each and every request should pass through the corresponding HTTP Module to reach to HTTP Handler, this list of module are configured by the HTTP Application.
Now, the concept comes called “HTTP Pipeline”. It is called a pipeline because it contains a set of Http Modules (For Both Web. config and Machine. config level) that intercept the request on its way to the Http Handler. HTTP Modules are classes that have access to the incoming request. We can also create our own HTTP Module if we need to handle anything during upcoming request and response.
All the request now passes from  http Module to  respective HTTP Handler then method and the ASP.NET Page life cycle starts.  This ends the IIS Request processing and start the ASP.NET Page Lifecycle.
The short overview of IIS Request how the request get processed in backend. 
When client request for some information from a web server, request first reaches to HTTP.SYS of IIS. HTTP.SYS then sends the request to respective Application Pool. Application Pool then forward the request to worker process to load the ISAPI Extension which will create an HTTP Runtime Object to Process the request via HTTP Module and HTTP Handler. After that the ASP.NET Page Life Cycle events start.

No comments:

Post a Comment