We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Question regarding Application_End Event

Medium Priority
Last Modified: 2012-05-06
In the past I have often used the Application_End event of the "global.asax" file. However, I have moved some of my code into a reusable library. Ideally I would like to dynamically register a handler for the application end event to do some clean-up routines.

Is there a way to do this? If not, what alternatives should I consider?
Watch Question

I think you may call the library from within your Application_End to do the processing.  However, the below post may be useful too.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


I thought that would probably be the case. Thanks for the link, the events table is rather useful.

If I were to implement the "IDisposable" interface on the class which requires the clean up, is the "Dispose" method guaranteed to have been called prior to the web application ending and/or recycling? Or would this be a little hit and miss?
Using iDisposable would work, but you would not guarantee that the Dispose() method is called exactly at the App_End event. I am pretty sure all of the objects connected to the application get garbage collected  on application end, so the Dispose() would be called eventually (probably within seconds after app_end).

What I would recommend is to create an event(OnApplicationShutdown) and implement the IDisposable interface AND implement a distructor.  The distructor method should put a message in the log to notify the programmer that the class is being used wrong (The Distructor was not called)

Google "iDisposable pattern" and you will see many examples.


Provided that the IDisposable interface is invoked before the application terminates, I should be fine.

Is it possible to have the class library define the OnApplicationShutdown event, or would it still be necessary for client code to include some sort of install method? I don't have too much of an issue with this, but obviously automatic would be better if possible.
You can refer the below two links.  There is one more before the Dispose, called the Application_EndRequest.


Application_EndRequest would cause the clean up code to occur for every single request though.

I only want the clean up code to occur when the application is being terminated (i.e. when web server is shut down, or when the server decides to recycle the application).
The Dispose would also do the same for every HttpApplication instances, and only Application_End will be called once.  Al though this is link points to 1.1, the reference as in # 23642961 is for 2.0 / 3.5, the over architecture may remain the same.

Quote from this link:
"The Dispose method is different from the Application_OnEnd event because it is always invoked on all HttpApplication instances within an application. Application_OnEnd is raised only once during an application's lifetime, on the last instance of HttpApplication that is torn down. Use Application_OnEnd only to clean up states or resources shared by all pipeline instances, such as the use of the ApplicationState object. Do not use it to clean up local variables, because local variables are not shared by multiple HttpApplication instances."


I have static object instances which implement "IDisposible" and are stored using the "Application" object. So my object should live beyond the context of a request and last the duration of the application.

For example, one of these object's produces a cache of tasks which will be performed when the web application reaches a certain state. However, if the server gets shut down prior to completion, there is a set of clean up routines which must be invoked.

I guess the simplest approach is just to make sure that websites which use this library call a static version of the clean up method upon hitting "Application_End" (like you said in your first response).
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.