How to save a session before it ends in VB

dgloveruk used Ask the Experts™
I save a class with various properties into a the current user session like this :
    Public ReadOnly Property UserSession As cUserSession
            If HttpContext.Current.Session("UserSession") Is Nothing Then
                HttpContext.Current.Session.Add("UserSession", New cUserSession)
            End If
            Return DirectCast(HttpContext.Current.Session("UserSession"), cUserSession)
        End Get
    End Property

Open in new window

I would like to save details in the class to a database before the session is lost.  If I rely upon Session_End in global.aspx, the object is already lost when I go to save it.  I tried to implement idisposable in the cUserSession but this didn't fire.  Whilst I can see how I can manually save the contents of the session by user action or property change, I would like to only do that when the session is about to be lost.

Is this achievable?

Thank you,
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Never thought about this kind of persistence. http is stateless, thus normally you save changes to you session, when they occur. Stateless also implies that you don't have a reliable  session end event.
Also your IDisposable workaround is not what you want, cause it the dispose call is not related to a lost session. It is either called explicitly by you, which is equivalent to an explicit save, or sometime later by the garbage collector. At this time the session can be dead or closed for already a long time.

The only thing you can do: You need a separate observer thread, which tracks your session age and state. BUT: ASP.NET is not built for running such a permanent thread, here you would need a Windows Service. Otherwise it is possible, that you lose some sessions. This implies that you to IPC with it and thus you need to persist the session object anyway.

So in short: Save your session when something changes.


Hi Sta5an,
Thank you very much for your answer.  I had also considered a thread for tracking the age and also considered saving session whenever something changes but hoped or assumed that there might be something out the box to better facilitate these objectives.  The session is to facilitate a web based CRM system with the expectation that users may have different records open and different views.  Losing tracking of this isn't the end of the world, the user will merely have to get back to where they were (like rebooting windows in the good old days!) but it would be nice to retain the configuration back to a user table when it becomes apparent the session is about to be dropped for later recovery.
I was going to use XML to store most of this session configuration data since it seems to suit this and will cope better with system changes without calling upon changes to the way the session is saved on the database side, provided I cater for backward compatibility with the xml.
I will close this ticket unless you had any further comment?
Thanks again,

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial