In my application I save some data related to the users sessions in the database. When the user logoff I cleanup all his session data from the database. So far, so good.
But, when IIS is restarted, all the active sessions are invalidated without the chance to gracefully cleanup.
So, I need to identify when IIS is restarted to solve this situation.
I would just put something int the application_start event in the global.asax
http://www.codetoad.com/asp.net_globalasax.asp
Your app has restarted for some reason, and according to your description it’ll have to clean all the sessions out of the db regardless if it is from IIS restarting, or the app pool getting recycled etc.
You can’t trust your application to set a flag that IIS has signaled a restart in your application_end event, because there are times when it won’t fire. For example what happens if the server crashes, or if IIS gets killed outside of it’s control (taskkill).