Wednesday, July 17, 2013

Getting the most out of Global.asax

Global.asax is a class derived from HttpApplication class. This file is also called ASP.NET application file. Global.asax is responsible for handling application level events raised by ASP.NET and HTTPModules. This file is optional if you haven't created it, ASP.NET assumes you have not defined any application or session event handlers.

During the lifetime of your application, ASP.NET maintains pool of Global.asax derived HttpApplication instances. When the application receives an HTTP request, ASP.NET page framework assigns one of these HttpApplication instances to process the request. That instance is responsible for managing the request throughout its lifetime. When you see many number of requests coming to the application, many instances of HttpApplication instances is expected

Inside Global.asax file there are many methods you can use to make your lives easier when developing ASP.NET applications. This article will look at some of the methods you can use in your web applications.

ASP.NET automatically binds application events to handlers in Global.asax using 'Application_event' naming convention. (How to ASP.NET Application_Events Work - Rick Strahl and SO Question).

Before digging into Global.asax file you must understand the sequence of how each method is called. Below diagram extracted from stackoverflow shows the sequence.


Global.asax event sequence

Application_Start and Init (source)
Application_Start is a special method which doesn't represent HttpApplication events (Also Application_End). ASP.NET calls them once for the lifetime of the application, not for each HttpApplication instance. On the other hand, Init is called once for every instance of HttpApplication after all modules have been created.

Application_End
Application_End will fire when IIS Pool is recycled or the application is unloaded. (If a dependent file such as web.config gets changed, application will reload).

PreSendRequestHeaders (link)
Occurs just before ASP.NET sends HTTP headers to the client. 

BeginRequest
First event of the HTTP pipeline chain of execution when ASP.NET responds to a request

EndRequest
Occurs as the last event 

Error handling
Resources You can handle application errors inside Application_Error method


Accessing Session Data inside Global.asax
http://stackoverflow.com/questions/765054/whens-the-earliest-i-can-access-some-session-data-in-global-asax
http://stackoverflow.com/questions/5977285/set-session-variable-in-application-beginrequest?lq=1

Access Global.asax properties some outside (SO)
 
Resources
MSDN, TechRepublic, Application life cycle for IIS 5.0 and IIS 6.0, Application life cycle for IIS 7.0
Check Create Custom HTTP Modules

0 comments:

Post a Comment

Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook