ASP.NET Complete Life Cycle

URL: http://aspalliance.com/1288_ASPNET_Complete_Life_Cycle

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.

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值