Session_OnEnd is not being called

M

Martin

Hi,

Since I went ASP.NET with my global.asa (making it a global.asax) the
application events are called just fine as well as the Session_OnStart event
but the Session_OnEnd event is not. What is wrong? My global.asax looks like
this:

<%@ Application src="app.cs" Inherits="mwte.App" %>


This is my code behind file (app.cs):

namespace mwte
{
using System;
using System.IO;
using System.Web;
using System.Xml;
using System.Web.SessionState;

public class App : System.Web.HttpApplication
{
public void SaveSessionInfo()
{
}

// application event handlers
protected void Application_OnStart(Object Sender, EventArgs e)
{
}

protected void Application_OnEnd(Object Sender, EventArgs e)
{
}

// other application event handlers left out for brevity

// session event handlers
protected void Session_Start(Object sender, EventArgs e)
{
}

protected void Session_End(Object sender, EventArgs e)
{
}

}
}


The documentation says that for the End event to occur, session state mode
must be InProc. It should be, in my web.config it says:

<configuration>
<system.web>
<sessionState mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424" stateNetworkTimeout="10"
sqlConnectionString="data source=127.0.0.1;Integrated Security=SSPI"
cookieless="false" timeout="20" />
</system.web>
</configuration>


Any insightswould be appreciated.

Martin.
 
M

Martin

Natty,
did you wait 20 sec. after the last user request as it defined in your
web.config?

No, I assumed that 20 was the default 20 minutes timeout for a session. And
when I call Session.Abandon, I expect Session_OnEnd to kick in immediately
(this is how it worked in classic ASP).

The strange thing is that I did get 2 or 3 Session_OnEnd events at
unsuspected times after I posted the problem. But when I invoke the Abandom
method, wait for well over 20 minutes, than look at the log to see if
Session_OnEnd ever occured it appears it didn't. It seems to behave very
irrational.


I looked at your web log the other day and I found it quite informative yet
could not keep up reading it because of the bad English that was kicking my
mind around two or three times every sentense. You should have your articles
edited by someone more comfortable with the English language and you will
have more readers.

Regards, Martin.
 
S

Steve C. Orr [MVP, MCSD]

The session_end event is normally called after the server has not gotten any
page requests from the user for 20 minutes (default, configurable in your
web.config).
The Session_End won't fire in a few circumstances, such as when you
end the session prematurely (i.e. pressing the stop button in VS.NET) or if
you aren't using the standard InProc sessions.

Here's more details for you:
http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=7504
 
M

Martin

Here's more details for you:

Hmm... Good source! It still doesn't all make sense to me yet what I am
experiencing. The time-out session end may have an access rights problem
with the log file though because it is called from a different thread as it
says in the FAQ. That would explain that part. But why doesn't
Session.Abandon() trigger Session_OnEnd ? It should.

Thanks for the link.

Martin.
 
S

Steve C. Orr [MVP, MCSD]

Session.Abandon doesn't invoke Session_End. That is by design. I'm not
saying that's a good or bad design, that's just how they made it. But it
shouldn't matter much; you can move your Session_End code into a separate
procedure and then call that procedure from Session_End and wherever you
call Session.Abandon.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com
 
A

Alvin Bruney

Session.Abandon doesn't invoke Session_End. That is by design.

No, that's not entirely correct. Session.Abandon may call session_end if the
mode is set to inproc. It's easy to draw the wrong conclusions because if
you build a page, place session.abandon() as the first line and put a break
point in session_end it will not fire. This slide of hand causes one to
conclude, rather erroneously, that session_end isn't fired when session
abandon is invoked.

The Abandon method will destroy the session by sending a new session id
cookie with an old expiration date. Then, abandon destroys the dictionary
associated with the session. Whether session_end event fires next is
entirely dependent on the state of the session id. Here is how they are
related. If the session id is regenerated, session end event fires. If the
session id is not regenerated, session end fails to fire. So in one case,
calling session abandon will invoke the session end event.

Session ID regeneration is peculiar (complicated) and optimized to support
the stateserver architecture. A new session id is regenerated if the
dictionary associated with the session has been written to at least once AND
at least one request has been completed successfully otherwise session id is
maintained even though the request may have terminated, and abandon has been
invoked. In that case, the existing session id is reused until the browser
is closed even though a new page is requested. With a new session id,
session end event fires.

In the OP's case, if the dictionary object isn't being written to OR a
request has not been completed successfully and abandon is called, session
end event will never fire because, as explained above, the same session id
is being recycled. To prove that this is the case, you will need to set up a
test case where you write to session in the page load. This satisfies one
condition. To satisfy the other condition, drop a button on your page and in
its event handler, call
session abandon. The successfull request will be the initial page load. With
both conditions satisfied, session end will fire.

Here is the complete test case example
protected System.Web.UI.WebControls.Button Button1;


private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

//removing the next line of code causes the

//condition to only be partially satisified

Session["test"] = "test";



//session end will not be called if the next line is uncommented

//because the conditions are only partially satisified

//Session.Abandon();

}



private void Button1_Click(object sender, System.EventArgs e)

{

//session end will be called here because both

//mutually inclusive events satisfy the condtion

Session.Abandon();

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top