Basic To Advanced - ASP.NET Life Cycles (Application Life cycle)

Introduction

ASP.NET defines two important lifecycles under MVC framework. The first one is the Application life cycle and the second one is the Request life cycle. It is important to understand both the lifecycles because it will help us perform the tasks at the beginning of the application until the application terminates. In a couple of blogs, I'll try to cover the below concepts,

This Blog
  • Application Life cycle
  • Request Life Cycle
  • Understanding Application life cycle

Next Blog

  • Request Life Cycle
  • Understanding Application life cycle
  • Modules
  • Handlers
  • Asp.Net Context objects
  • HttpApplication objects
  • HttpRequest objects
  • HttpResponse Objects

Application Life Cycle

Application life cycle tracks the life of the application from the moment it starts to the moment it terminates.

Request Life Cycle

Request life cycle defines the path of HTTP Request from the moment request is received to the moment response is sent.


Understanding Application Life Cycle

Now, the obvious question arises: Why do we care to know about the application lifecycle? This is because if you have any off-configuration task or if you need to release resource when the application is stopped, you can perform such task using Application lifecycle.

Global application class (formally known as the Global.asax file) has the most important role in both the lifecycles. It provides us notification through the methods defined in it.

Note Global.asax file is known as Global application class.

Global.asax.cs is the associated code-behind file

Below is the default code present in the Global Application Class. The default global application class is called MVC Application and is derived from System.Web.HttpApplication class. The default implementation of global application class contains only one method Application_Start, but there is another method Application_End available.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. namespace TestApplication {  
  8.     public class MvcApplication: System.Web.HttpApplication {  
  9.         protected void Application_Start() {  
  10.             AreaRegistration.RegisterAllAreas();  
  11.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  12.         }  
  13.     }  
  14. }  
Application_Start 

This method is the entry point of the application, and is the first method which is called.

Application_End 

This is the exit point of the application. This is the last method which is called before the application terminates legally. This is the point where we can release certain off-configuration resources.

Note

Illegal termination of application will not execute the Application_End method

To test the Application_Start method, we need to update the above Application_Start method code with the below code.
  1. protected void Application_Start() {  
  2.     System.Diagnostics.Debugger.Break();  
  3.     AreaRegistration.RegisterAllAreas();  
  4.     RouteConfig.RegisterRoutes(RouteTable.Routes);  
  5. }  
Now, run your application. System.Diagnostics.Debugger.Break(); will break the application at the point it is written as shown below.



To test the Application_End method, write the below code into the MVCApplication class.
  1. protected void Application_End() {  
  2.     System.Diagnostics.Debugger.Break();  
  3. }  
Testing the Application_End method is a bit tricky because it will only invoke when the application terminates legally. So stopping the application will detach the debugger from the application before the Applicaiton_End method is called. To test the Application_End method, we need to stop our site using IIS Express directly, as shown below.




So the whole application lifecycle looks as below

Ebook Download
View all
Learn
View all