Ahmed ilyas
Ahmed ilyas

Reputation: 5822

Access Session on End?

I am trying to "log" forcefully when a user has been inactive and or the session has ended (either by inactivity or more importantly, when the browser has closed). I dont want to use any silly AJAX solution to perform a post every few minutes for "im alive" or call when the browser is closed.

I was under the impression that if you store an object in Session, and you reach the Session_End event, then you will not be able to gain access to anything stored in Session as its ended. But from some testing I have done, it appears that this is probably the last chance you can obtain access to the object.

Could this be true? is it reliable?

using ASP.NET 4.0 here.

Upvotes: 3

Views: 932

Answers (2)

ChrisLively
ChrisLively

Reputation: 88074

Typically there are two things done.

The first is that a javascript timer is added to the client, not as a heartbeat, but rather as a reminder. If they are close to the session ending, then it simply says "session is about to end. Are you still there?" If so, then it does the "silly" post to ensure the server keeps the session going. This is purely to be nice to your users.

The second (and point of your question) is that you put something in session_end in order to clean up the session. Reliable? well.. most of the time.

Session_End won't run if the app pool is recycled. However, assuming the app pool is ok then yes it will execute when the session expires. The app pool can be recycled for a LOT of reasons ranging from the app crashing to exceeded memory usage to simply because it's been a while since the last reset. This is configurable in IIS.

Would I trust session_end? No. Not 100%. Of course, I wouldn't put anything inside of a session object that would require me to trust it 100% anyway.

Upvotes: 3

PAULDAWG
PAULDAWG

Reputation: 790

For logging the timeout You can use the Global events to log at timeout. See this link for order of events http://www.techrepublic.com/article/working-with-the-aspnet-globalasax-file/5771721

Upvotes: 0

Related Questions