ASP.NET MVC: Access session data in Session_End or in event just before

3.9k Views Asked by At

I've already seen a similar question to this one posted here. I have essentially the same question, but with a small twist.

I'm trying to access Session information from within the Session_End method, but HttpContext is null by the time I get there, so I'm not finding any real way to get to it; it's almost like it's already been completely erased by the time Session_End fires.

A solution in the related question says I can use this.Session to reference the session information I need, which accesses the HttpApplication.Session variable from the HttpApplication class Global inherits from. But in my application, we've redefined the Session variable ourselves in Global to look like this:

    public new static HttpSessionState Session
    {
        get
        {
            return HttpContext.Current.ApplicationInstance.Session;
        }
    }

So for me, referencing this.Session will try to get information out of HttpContext, which is null.

So I guess I have 2 questions. First, is there an event that fires before Session_End but before the Session data is cleared? And second, if the answer to the first question is no, is there still a way to access that data from Session_End?

Thanks.

1

There are 1 best solutions below

0
On

bear in mind the session_end will raised only if you are using in_proc session state mode: if you deploy your web app on a server farms it won't be raised. anyway even if I don't know what you want achieve it would be better handle the session end within an HttpModule. have a look at the link below:

http://www.codeproject.com/KB/aspnet/SessionEndStatePersister.aspx