tags:

views:

678

answers:

3

Officially the HttpSession event End is handled only in the Global.asax file, but I was wondering if is it there a way, although not officially (eg. Reflection) to handle the event in a different way.

A: 

i am not that expert on this matter, but i think you can use HTTPModule, as it allow to have access to all the events pf hte ASPX page, here is a link to nice article on creating HTTP Modules.

http://www.15seconds.com/Issue/020417.htm

hope this helps.

+2  A: 

You could do something like this, but from what I have seen, it can be a nightmare:

public class MyHttpModule : IHttpModule, IRequiresSessionState
{
public void Init(HttpApplication app)
{
if (app.Modules["Session"] != null)
{
SessionStateModule session = (SessionStateModule) app.Modules["Session"];
session.Start += new EventHandler(Session_Start);
session.End += new EventHandler(Session_End);
}

app.PostRequestHandlerExecute += new EventHandler(Application_PostRequestHandlerExecute);
}

public void Dispose()
{
}


private void Session_Start(object sender, EventArgs e)
{
}

private void Application_PostRequestHandlerExecute(object sender, EventArgs e)
{
}

private void Session_End(object sender, EventArgs e)
{
}
}

There are multiple instances of your module, and they do not all have 'Session' in their list of loaded modules. I think there might also be multiple instances of the Session module, so the one whose End event you registered for in your first instance may not be the one that fires the End event later on.

Nebakanezer
yes, I've tried this solution in the past, but it seems not working: I haven't inspected the M$ .NET framework's code in depth (nor Mono's), but i believe that there's a mechanism to call the End event only to the destination of Global.asax...
Antonello
same for me, Global.asax Start works, but if I try to reference Session module (and there is an instance of this module actually) then Start event just doesn't seem to work. And things are indeed more strange for the End event (Mono has a comment in its sources: "This event is public, but only Session_[On]End in global.asax will be invoked if present.")
IgorK
+1  A: 

This is not something that can be easily accomplished in an HttpModule, as the Session_OnEnd event may occur outside the scope of a request to the system. You are probably better off handling this through a custom Session State provider, where your Session management code is what triggers the Session_OnEnd event.

Jeff Fritz