Question regarding Application_End Event

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?
LVL 13
numberkruncherAsked:
Who is Participating?
 
CB_ThirumalaiCommented:
I think you may call the library from within your Application_End to do the processing.  However, the below post may be useful too.
http://www.brainbell.com/tutorials/ASP/Overriding_HttpApplication.html
0
 
numberkruncherAuthor Commented:
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?
0
 
MogalManicCommented:
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.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
numberkruncherAuthor Commented:
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.
0
 
CB_ThirumalaiCommented:
You can refer the below two links.  There is one more before the Dispose, called the Application_EndRequest.
http://msdn.microsoft.com/en-us/library/ms178473.aspx
http://msdn.microsoft.com/en-us/library/bb470252.aspx 
0
 
numberkruncherAuthor Commented:
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).
0
 
CB_ThirumalaiCommented:
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.

http://msdn.microsoft.com/en-us/library/a0xez8f2(VS.71).aspx
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."
0
 
numberkruncherAuthor Commented:
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).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.