Advertisment

ASP.NET Application Request Cycle

author-image
CIOL Bureau
Updated On
New Update

ASP.NET is a powerful and flexible engine for processing web requests. It is a platform for building managed Web applications. The entire ASP.NET engine is completely built in managed code and all extensibility is provided via managed code extensions.

Advertisment



Application Request Cycle:

ASP.NET takes an incoming HTTP request and passes it through its internal pipeline to an endpoint where a code can be attached to process that request. This engine is separated completely from the web server. The HTTP Runtime is a component that can be hosted in applications outside IIS or on any server side application.

Advertisment

The entire ASP.NET engine is completely built in managed code and all of the extensibility functionality is provided via managed code extensions. With few limitations, ASP.NET can perform tasks that previously were the domain of ISAPI extensions and filters on IIS. ASP.NET interacts with IIS through an ISAPI extension that hosts .NET and ASP.NET runtime. ISAPI is a low level win32 API that provides the core extensions that hosts .NET and through it the ASP.NET runtime. ISAPI provides the core interface from the web server. ASP.NET uses the unmanaged ISAPI and provides the core interface from the web server. ASP.NET also uses the unmanaged ISAPI code to retrieve input and send output back to the client. The content that ISAPI provides is available via common objects like HttpRequest and HttpResponse that expose the unmanaged data as managed objects.

 

ASP.NET web request begins on the browser with a user typing in a URL and submitting a HTML form (a POST request) or an application making web method call to an ASP.NET based web service. The web server on the server side, Internet Information Server 5 or 6 picks up the request. The request goes through an ASP.NET application extension (eg: .aspx, asmx etc) that is registered and points at aspnet_isapi.dll. HttpHandlers are responsible for processing the request. Depending on the extension, ASP.NET routes the request to an appropriate handler that is responsible for picking up requests. HttpHandlers are mapped to point at the ASP.NET ISAPI extension in IIS, and configured in web.config to get routed to a specific HTTP Handler implementation. HttpHandler is a .NET class that handles a specific extension.

Advertisment

These mappings can be seen in the IIS Service manager as in the figure below.

Advertisment

The worker processes ASPNET_WP.EXE in IIS5 and W3WP.EXE in IIS6 host the .NET runtime. The ISAPI DLL calls into small set of unmanaged interfaces via low level COM that eventually forward call to an instance subclass of the ISAPIRuntime class. The transfer of the ISAPI request into the HTTP Pipeline of ASP.NET uses a number of undocumented classes and interfaces and requires several factory method calls. The first entry point to the runtime is the undocumented ISAPIRuntime class which exposes the IISAPIRuntime interface via COM to a caller. Each Web Application runs in its own AppDomain with the caller holding a reference to an IISAPIRuntime interface that triggers the ASP.NET request processing.

Advertisment

ProcessRequest() method in ISAPIRuntime class in turn calls HttpRuntime.ProcessRequest that does several important things. It helps to:

  • Create a new HttpContext instance for the request
  • Retrieve an HttpApplication Instance
  • Call HttpApplication.Init() to set up Pipeline Events
  • Init() fires HttpApplication.ResumeProcessing() which starts the ASP.NET pipeline processing
 
Advertisment

HttpContext: 

The HttpContext object represents the context of the currently active request as it contains references to all of the essential objects that are required during the request lifetime (Request, Response, Application, Server, Cache). The Context is available throughout the lifetime of the request and accessible via the static HttpContext.Current property. The HttpContext object also contains a very useful Items collection that can be used to store data that is request specific. The context object gets created at the begining of the request cycle and released when the request finishes. So the data stored there in the Items collection is specific only to the current request.

HttpApplication: 

Advertisment

HttpApplication is the outer container for a specific web application and it maps to the class that is defined in Global.asax. It’s the first entry point into the HTTP Runtime. Each request is routed to an HttpApplication object. The HttpApplicationFactory class creates a pool of HttpApplication objects for ASP.NET application depending on the load on the application and hands out references for each incoming request.

HttpApplication’s primary purpose is to act as the event controller of the Http Pipeline and so its interface consists primarily of events. These events are also implemented in the Global.asax file (eg: Application_BeginRequest, Application_AuthorizeRequest).

The HttpApplication does not know anything about the data being sent to the application. It triggers events and passes information via the HttpContext object to the called methods. The actual state data for the current request is maintained in the HttpContext object mentioned earlier. It provides all the request specific data and follows each request from beginning to end through the pipeline.

HttpModules: 

HttpModules are actual event handlers that handle specific HttpApplication events. HttpModules can look at each incoming Web request and perform an action based on the events that fire. Modules are great to modify request or response content, to provide custom authentication or otherwise provide pre or post processing to every request that occurs against ASP.NET in a particular application. Many of ASP.NET’s features like the Authentication and Session engines are implemented as HTTP Modules. Every inbound HttpRequest passes through HttpModule.

HttpHandlers: 

HttpHandlers handle application level request processing. Each HttpHandler enables processing of individual HTTP URLs or groups of URL extensions within an application. HttpHandlers have the same functionality as ISAPI extensions with a much simpler programming model. An HttpHandler can be either synchronous or asynchronous. A synchronous handler does not return until it finishes processing the HTTP request for which it is called. An asynchronous handler usually launches a process that can be lengthy, and returns before that process finishes. .aspx and .asmx are examples of HttpHandler.

Summary:

To summarize, ASP.NET Application life cycle begins with IIS receiving the request. IIS looks up a script map extension and maps to aspnet_isapi.dll. ASPNET_ISAPI.dll invokes the worker process and in-turn .NET runtime is loaded. HTTPContext object gets created by passing worker request as input and then the HTTPApplication instance is retrieved from the worker pool. HttpHandlers are called to process the HttpRequest events. Finally, control returns to pipeline and post request events fire.

(The author is a technical architect with FCG Software Services (India) Pvt. Ltd.)

tech-news