[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Question regarding Application_End Event

Posted on 2009-02-08
8
Medium Priority
?
725 Views
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?
0
Comment
Question by:numberkruncher
  • 4
  • 3
8 Comments
 
LVL 12

Accepted Solution

by:
CB_Thirumalai earned 1500 total points
ID: 23586846
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
 
LVL 13

Author Comment

by:numberkruncher
ID: 23595913
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
 
LVL 21

Expert Comment

by:MogalManic
ID: 23631910
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 13

Author Comment

by:numberkruncher
ID: 23642627
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
 
LVL 12

Expert Comment

by:CB_Thirumalai
ID: 23642961
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
 
LVL 13

Author Comment

by:numberkruncher
ID: 23642984
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
 
LVL 12

Expert Comment

by:CB_Thirumalai
ID: 23643027
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
 
LVL 13

Author Comment

by:numberkruncher
ID: 23643112
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question