ASP.NET Complete Life Cycle
 
Published: 06 Jun 2007
Abstract
This article gives an overview of the ASP.NET life cycle and the events involved in the execution of the page.
by Srinivas Jadhav
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 24377/ 59

Introduction

Since too many resources are shared on the process level (i.e. IIS, inetinfo.exe) and it is too easy for an error to bring down the entire server process, ASP.NET provides an out-of-process execution model, which protects the server process from user code.

Life cycle of an ASP.NET application starts when the browser requests a page from the Web Server.

ISAPI.dll resides inside the Web Server, which examines the file extension of the requested file and determines which ISAPI extension should handle the request.

ISAPI.dll uses a named pipe and forwards a request to IIS where it runs inetinfo.exe to an instance of ASP.NET worker process aspnet_wp.exe. The worker process uses an instance of the HttpRuntime class to process the request.

By default, there will be only one worker process in use at a time.

If the file extension has been mapped successfully then a class named ApplicationManager is called which creates an Application Domain.

Application Domain

Application Domain provides isolation between different applications for global variables and allows each application to be unloaded separately.

Multiple Application Domains can run in a Single Process.

Why Isolation?

Single process can have multiple threads, memory leak, out-of-bound memory access, null object referencing, etc. If one of the applications crashes because of these, then all the application crashes; therefore, Isolation of application is required.

HostingEnvironment

An instance of a class named HostingEnvironment is created which resides in Application Domain.

Once the Application Domain is created and HostingEnvironment object is instantiated, Http requests are passed to an instance of the HttpRuntime class. HttpRuntime object examines the request and figures out which application it was sent to (i.e. virtual directory). It the uses HttpApplicationFactory to either find or create an HttpApplication object to process the request.

ASP.NET creates and initializes the core objects such as HttpContext, HttpRequest and HttpResponse.

After all the core objects are initialized, the application is started by creating an instance of the HttpApplication class. HttpApplication holds a collection of Http module objects

Application is started by creating an instance of the HttpApplication class. If the application contains Global.asax, it overrides the Global.asax file, which is derived from the HttpApplication.

The first time an ASP.NET page or process is requested in an application, a new instance of HttpApplication is created. To maximize performance, HttpApplication instances might be reused for multiple requests.

An instance of HttpApplication processes only one request at a time.

The following are the events which are called during the application life cycle.

1. Application_Start: It is called when the first resource in an ASP.NET application is requested.

2. Application_event:  It is called at the appropriate time during the application life cycle.

Some of the Application events are as follows.

Application_BeginRequest: Whenever ASP.NET responds to the Request, it will be the first to occur in the Http Pipeline.

Handler

Listing 1

public event EventHandler BeginRequest; 

Application_AuthenticateRequest: Occurs whenever the identity of the user has to be established.

Handler

Listing 2

public event EventHandler AuthenticateRequest;

Application_AuthorizeRequest: Occurs whenever the identity of the user has been verified.

Handler

Listing 3

public event EventHandler AuthorizeRequest;

Application_Error: Occurs when ever the unhandled exception is thrown.

Handler

Listing 4

public event EventHandler Error;

Application_EndRequest: Whenever ASP.NET responds to the Request, it will be the last to occur in the Http Pipeline chain.

Handler

Listing 5

public event EventHandler EndRequest;

3. HttpApplication.Init: called once for every instance of the HttpApplication class.

4. Application_End: Called once per lifetime of the application before the application is unloaded.

Conclusion

This article briefly examined the lifecycle of an ASP.NET application. It also discussed some of the HttpApplication events.



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-08-24 10:42:28 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search