2

I have some code that I want to force to run whenever my IIS app gets reloaded, stopped or any other event that would reset the state of some static properties in a business class.

I've made the class implement IDisposable and I tried adding a class destructor, but neither run when I upload a new dll to the server. How can I, from inside this class, register a method to run before the application is unloaded?

powlette
  • 1,800
  • 20
  • 41
  • Side note: doing non-trivial work during shutdown/restart (and especially relying on it to always happen) is dangerous idea. While often your code will be notified in some form sometimes process will be simply teared down without any chance for user's managed code to run. – Alexei Levenkov Mar 27 '13 at 23:43
  • "reset the state of some static properties in a business class" assuming it means "commit some non-trivial changes to external storage"... Otherwise please comment on what you actually want to reset. – Alexei Levenkov Mar 27 '13 at 23:44
  • I'm giving you a virtual *slap* for calling a finalizer a destructor. Tsk tsk. Also, you'll have to be a bit more specific about what you're trying to do. – Simon Whitehead Mar 27 '13 at 23:54
  • I have counts in memory in a static collection that are collected while the app is running. I need to ensure those cached values are committed to the database before they're reset. That's what I'm trying to do. – powlette Mar 28 '13 at 00:16
  • You can't ensure anything when an application isn't terminated properly. Your best bet is to persist what you can while the application is alive and running well. – Simon Whitehead Mar 28 '13 at 00:24

1 Answers1

2

I would look at the events in Global.asax. This is typically where you will handle application level events. Here are a few links to get you started:

Application_Init or Application_Start might be good for your purposes?

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486