Sunday, March 9, 2008

Methods in Global.asax

This blog is intended to spread some light on the various methods which are available in global.asax file in ASP.NET. It’s very important to understand the methods in global.asax so that we as programmers can handle some application level events very efficiently. I said application level events and reason for using application word is that global.asax is an application level file and methods in it are used to handle application level events and these methods are not at all specific to any aspx page. Some of the common methods in the order in which they are executed are listed below

  • Application_Start
  • Application_BeginRequest
  • Application_AuthenticateRequest
  • Session_Start
  • Application_EndRequest
  • Session_End
  • Application_End
  • Application_Error

Now let’s see what is the major difference between these methods or events. Oh I forgot to say, these are actually events not methods which get raised when a particular event gets triggered. Before we see the various methods in Global.asax I would like to tell you that Global.asax is actually derived from a class called “HttpApplication”. The above listed methods are only a few methods which I am gonna talk about. The listing of other methods can be found at the end of the blog. Now lets see the above mentioned events one by one.


Application_Start
Application_Start event gets triggered only once during the life cycle of the application. This once happens when the first request for any resource in the application comes. Resource can be a page or an image in the application. When the very first request for a resource, say a web page, is made by a user “Application_Start” is triggered after which this event is not at all executed. If by any chance the server where the application is hosted is restarted then this event is fired once again i.e. when the very first request for any resource in the application is made after the server is reset.


Application_BeginRequest
“Application_BeginRequest” is the second event which gets fired after “Application_Start”. Unlike the “Application_Start”, “Application_BeginRequest” is triggered for each and every request which comes to the application. Since this method is fired for any request made to the application you can use this method to keep track of what and all resources are accessed through this method.


Application_AuthenticateRequest
“Application_AuthenticateRequest” is the next event in line which is triggered after “Application_BeginRequest” is triggered. “Application_AuthenticateRequest” is also fired for each and every request. This event can be used to write code in scenarios where you want to do something when the user is getting authenticated.


Session_Start
The next event in line which gets triggered after “Application_AuthenticateRequest” is “Session_Start”. Session start event is fired only when a new session for a user starts. Once “Session_Start” for a user is fired then if the user makes subsequent request to any resource within the application this event is not at all triggered. The event is triggered only when the user’s session expires and then the user tries to access any resource in the application again.
This event can be used when you want to do something when the user visits you site/application for the first time or when his session starts. This event doesn’t get triggered if you are not using sessions which can be disabled in the web.config.


Application_EndRequest
The next event in line which gets fired once the request for the user is processed is “Applicatin_EndRequest”. This event is the closing event of “Applicatin_BeginRequest”. This event is also fired for each and every request which comes for the application.


Session_End
The closing event of “Session_Start” event. Whenever a user’s session in the application expires this event gets fired. So anything you want to do when the user’s session expires you can write codes here. The session expiration time can be set in web.config file. By default session time out is set to 20 mins.


Application_End
The same as “Application_Start”, “Application_End” is executed only once, when the application is unloaded. This event is the end event of “Application_Start”. This event is normally fired when the application is taken offline or when the server is stopped.


Application_Error
Now we come to the last event mentioned in this blog and that is “Application_Error”. This event gets fired when any unhandled exception/error occurs anywhere in the application. Any unhandled here means exception which are not caught using try catch block. Also if you have custom errors enabled in your application i.e. in web.config file then the configuration in web.config takes precedence and all errors will be directed to the file mentioned in the tag.

Lets see with an e.g. how these events get fired.
Suppose “A”, “B” and “C” are users who are going to access a site named “My Site”. “A” is the very first user to visit “My Site” and he/she is accessing “productlist.aspx” page. At this time the flow of the request is as follows. The “Application_Start” event is triggered, since “A” is the very first user to visit the application, after this “Application_BeginRequest”, then “Application_AuthenticateRequest”, then “Session_Start”, “productlist.aspx” page level events are processed and then “Application_EndRequest” event is triggered. After accessing “productlist.aspx” if “A” access some other page then for those page request the flow will be first “Application_BeginRequest”, “Application_AuthenticateRequest” then the page processing (page level events) and then “Application_EndRequest”. For every subsequent request this pattern is followed.


When “B” accesses some resource in the site, say “default.aspx”, then first “Applicatin_BeginRequest”, second “Application_AuthenticateRequest”, third “Session_Start” then “default.aspx” page level events are executed and after that “Application_EndRequest” is executed. After accessing “default.aspx” “B” access “productlist.aspx” then first “Application_BeginRequest”, second “Application_AuthenticateRequest” then “productlist.aspx” and then “Application_EndRequest” event is triggered. He refreshes the page the same events are executed in the same order.

The above same process is repeated for “C” also.

Suppose you have an unhandled exception and you don’t have custom errors enabled in web.config then when a user accesses a resource the flow will be first “Application_BeginRequest”, “Application_AuthenticateRequest”, page level event and an error occurs in the page then it goes to “Application_Error” after that “Application_EndRequest”.
The order mentioned above is how the events are triggered. So with this I hope you would have got a clear idea on how these events are triggered.

Some other events which are part of the HttpApplication class are as follows

  • PostAuthenticateRequest
  • AuthorizeRequest
  • PostAuthorizeRequest
  • ResolveRequestCache
  • PostResolveRequestCache
  • PostMapRequestHandler
  • AcquireRequestState
  • PostAcquireRequestState
  • PreRequestHandlerExecute
  • PostRequestHandlerExecute
  • ReleaseRequestState
  • PostReleaseRequestState
  • UpdateRequestCache
  • PostUpdateRequestCache
  • LogRequest. (Supported in IIS 7.0 only.)
  • PostLogRequest (Supported in IIS 7.0 only.)

46 comments:

  1. Very nice, systematic description is easy words… Thanks a lot…
    -- Nazir

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. excellent clarity in explanation... kindly put more!!

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hi sandeep,

    Great explanation, it will nice if you can give more details on HttpApplication events.

    Thank you,

    Ananth

    ReplyDelete
  6. find the detailed explanation on this page...
    http://articles.techrepublic.com.com/5100-10878_11-5771721.html

    ReplyDelete
    Replies
    1. This is a dummy spam URL. Note the .com.com

      Delete
  7. Excellent explanation with simple and easy example..

    ReplyDelete
  8. good description and explanation.

    ReplyDelete
  9. Very good. This article is really helpful to me.
    Thank You.

    ReplyDelete
  10. This article is best article from many of one

    ReplyDelete
  11. Sir, i want to know;Here there are three users A,B,C.
    Application_Start() method is calling only once, becoz all three users are logging on same machine?

    Now i want code of session_start() method, then i ll try for this.

    ReplyDelete
  12. Very Useful info. Thanks.

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Good Post!

    I also posted about HttpApplication object,

    Application and Global.asax

    Hope it contributes

    Thanks!

    ReplyDelete
  15. very Good article.

    one comment though:
    The Session_End event handler is going to fire only if the session-state-mode=inProc (this is also the default setting in web.config)

    ReplyDelete
  16. hi boss......your explanation great clear.....so keep touch on our blog and update also more information.........

    thanks for give precious time......

    ReplyDelete
  17. Any unhandled here indicates exemption which are not captured using try capture prevent.

    ReplyDelete
  18. very nice & easy to understand.

    ReplyDelete
  19. WOW nice systematic step by step procees.

    ReplyDelete
  20. I must thank you for the efforts you have put in writing this blog. I am hoping to view the same high-grade content by you in the future as well. In fact, your creative writing abilities has encouraged me to get my very own website now ;)
    Tech news

    ReplyDelete
  21. Thanks for the interesting blog that you have implemented here. Very helpful and innovative. Waiting for your next upcoming article.
    Java training in chennai
    Java training institute in chennai
    Java course in chennai
    Java training classes
    Java training
    Java programming classes
    core java coure
    <a

    ReplyDelete
  22. Great blog you have here..best It’s difficult to find good quality writing like yours these days. I truly appreciate people like you! Take care!!

    ReplyDelete

  23. Really, this article is truly one of the best, information shared was valuable and resourceful Very good work thank you.
    tally training in chennai

    hadoop training in chennai

    sap training in chennai

    oracle training in chennai

    angular js training in chennai

    ReplyDelete
  24. many years. Cheapest and fastest Backlink Indexing Best GPL Store insta to mp4

    ReplyDelete
  25. I really relished every bit of it and I've marked to ensure that the blog post certain thing new.
    Cargo Services Dubai to Pakistan
    Glass Works in Dubai

    ReplyDelete
  26. What a commendable work you have done, with simplest of language. I can’t resist myself to
    leave a comment and trust me it’s hard to impress me..
    wedding photography packages
    leather jacket

    ReplyDelete
  27. hey sandeep

    it was very informative article, do share something more like this and it is easy to understand
    thank you very much...!!

    ReplyDelete
  28. The methods in the Global.asax file in ASP.NET are used to handle application-level events efficiently. These methods are not specific to any particular ASPX page and are executed in a specific order. Here is a brief explanation of some common methods/events in the Global.asax file:

    Application_Start: This event is triggered only once during the application's lifecycle. It occurs when the first request for any resource in the application is made. After that, this event is not executed again unless the server hosting the application is restarted.

    Application_BeginRequest: This event is raised at the beginning of each request made to the application. It allows you to perform preprocessing logic or make decisions based on the incoming request.

    Application_AuthenticateRequest: This event is raised when the server is authenticating the user's request. It provides an opportunity to validate the user's identity and perform any necessary authentication-related tasks.

    Session_Start: This event is triggered when a new user session is started. It allows you to perform initialization tasks specific to the user session, such as setting session variables or initializing user-specific resources.

    Application_EndRequest: This event is raised at the end of each request made to the application. It allows you to perform post-processing logic or modify the outgoing response.

    Session_End: This event is triggered when a user session ends, either by timeout or by explicitly calling session abandonment. It can be used to perform cleanup tasks or log user session information.

    Application_End: This event is raised when the application is shutting down or being unloaded from memory. It provides an opportunity to perform any necessary cleanup tasks or release resources.

    Application_Error: This event is raised when an unhandled exception occurs in the application. It allows you to handle the exception, log error information, and customize error pages.

    These events are actually events rather than methods, as they are raised when a specific event occurs. The Global.asax file is derived from the "HttpApplication" class. There are additional methods/events available in the Global.asax file, and you can find a listing of them at the end of the blog.

    Overall, understanding these methods/events in the Global.asax file is crucial for handling application-level events effectively in SEO Jobs
    Visit Serafelagi.com!

    ReplyDelete
  29. I really like your blog .it is very informative for me thanks for sharing.If you want to know about Data Science so can visit : DATA ALCHEMY: TRANSFORMING INFORMATION INTO KNOWLEDGE WITH DATA SCIENCE

    ReplyDelete
  30. The content you find on RankBuilder is more than just a piece of writing. It’s a stream pool of practical tips and tricks for entrepreneurs and other digital marketing enthusiasts who want to become successful. By reading our blogs you will find that things that seem complicated to you will become easier for you, whether it is SEO or online advertising.

    ReplyDelete

Please provide your valuable comments.