tags:

views:

53

answers:

3

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.

+1  A: 

I would just put something int the application_start event in the global.asax

http://www.codetoad.com/asp.net%5Fglobalasax.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).

Kevin
I suspect (but verifying this would be smart) that he could put something in the Application_End event instead.
Brian
A: 

There is a DBNAME_Job_DeleteExpiredSessions job defined on SQL server where your session data is stored. If your SQL Agent service is running, the job will clear old sessions. More info

LymanZerga
A: 

What about session timeout? do you need to cleanup after timeouts? also keep in mind that you cannot really relay on the Session.End event - it only works for in-process sessions

mfeingold