Session_End and mysttries on when does it fires in Asp.Net

Hi,

Following my post on few interesting things about session in Asp.Net many people asked me about the Session_End event. You can read the earlier post here.

Many people have asked me on exactly what are the reason for which the Session_End event fires. So I thought I would write a post on the same.

While thinking of Session_End event one of the most important things is that the event is supported only when the session is maintained Inproc. The session_End event will not fire if you are using State server or SQL server.

Another important point to note is that Session_End event is fired by a worker process. This also means that Session_End event might not have all the permission that a normal page request has. So if you are trying to connect to the SQL server with Windows account, Session_End event might not have required permission, although you can connect to the SQL in Session_Start event.

Now when does the Session_End event fires?

Session_End will fire after a given amount of time where session has been inactive. The given time can be changed globally in the web.config file with the time out attribute or for individual session with the help of Session.Timeout property.

Session_End will also fire when someone calls the Session.Abondon method. Note that the Session_End event will not fire immediately. As said before the Session_End event is fired from worker process. Hence the session ID will also be same.

Remember the Session_End event will not fire in case the user closes the browser. HTTP is a stateless protocol. There is no way for the server to understand that the browser has been closed.

Also another very important thing to note here is that if you do not save anything in the session the Session_End event will not fire. There must be something saved in the session atleast once for the Session_End event to fire.

This also means that if you save something in the session in the first request and abandon the session in the same request the Sesison_End event will not fire, as there was nothing saved in the session ever.

Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 10/8/2007 5:43:35 AM

Thanks for details of session state ,its really interesting

Posted on 9/15/2008 11:37:14 PM

Good one. Nicely presented.

Posted on 10/2/2008 6:05:43 AM

Thanx for this nice article....

Posted on 10/2/2008 6:08:25 AM

Thanx for this nice article....

Posted on 10/28/2008 2:43:09 AM

Very useful...! Thanks!

Posted on 10/28/2008 2:43:20 AM

Very useful...! Thanks!

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia