Daniel Silveira
Daniel Silveira

Reputation: 42553

How to Identify that IIS Restarted?

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.

Upvotes: 2

Views: 1816

Answers (3)

mfeingold
mfeingold

Reputation: 7134

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

Upvotes: 0

Alex Polkhovsky
Alex Polkhovsky

Reputation: 3360

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

Upvotes: 0

kemiller2002
kemiller2002

Reputation: 115538

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).

Upvotes: 1

Related Questions