Sunday, November 11, 2007

ASP.NET Application Life Cycle (2)

Life Cycle Events and the Global.asax file
  • During the application life cycle, the application raises events that you can handle and calls particular methods that you can override. To handle application events or methods, you can create a file named Global.asax in the root directory of your application.
  • If you create a Global.asax file, ASP.NET compiles it into a class derived from the HttpApplication class, and then uses the derived class to represent the application.
  • An instance of HttpApplication processes only one request at a time. This simplifies application event handling because you do not need to lock non-static members in the application class when you access them. This also allows you to store request-specific data in non-static members of the application class. For example, you can define a property in the Global.asax file and assign it a request-specific value.
  • ASP.NET automatically binds application events to handlers in the Global.asax file using the naming convention Application_event, such as Application_BeginRequest.
  • The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance.

The following lists some of the events and methods that are used during the application life cycle. There are many more events than those listed, but they are not commonly used.

  • Application_Start
    Called when the first resource (such as a page) in an ASP.NET application is requested. The Application_Start method is called only one time during the life cycle of an application. You can use this method to perform startup tasks such as loading data into the cache and initializing static values.
    You should set only static data during application start. Do not set any instance data because it will be available only to the first instance of the HttpApplication class that is created.
  • Application_event
    Raised at the appropriate time in the application life cycle, as listed in the application life cycle table earlier in this topic.
    Application_Error can be raised at any phase in the application life cycle.
    Application_EndRequest is the only event that is guaranteed to be raised in every request, because a request can be short-circuited. For example, if two modules handle the Application_BeginRequest event and the first one throws an exception, the Application_BeginRequest event will not be called for the second module. However, the Application_EndRequest method is always called to allow the application to clean up resources.
  • HttpApplication.Init
    Called once for every instance of the HttpApplication class after all modules have been created.
  • Dispose
    Called before the application instance is destroyed. You can use this method to manually release any unmanaged resources.
  • Application_End
    Called once per lifetime of the application before the application is unloaded.
blog comments powered by Disqus