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 iden开发者_StackOverflow中文版tify 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).
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
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
精彩评论