[VB.NET]
When a user's Session expires, I'd like to remove certain things from
the Cache. For instance, some data items I've stored using the user's
SessionID, so using that, I could remove some of these things from the
Cache...
...if I could get to it.
I know how to iterate the Cache, but the Context object is Nothing
when the Session_End() event fires! Take a look at my code:
'-------------------------------------------------------------------------
' Session_End()
'-------------------------------------------------------------------------
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim oIEnumerator As IDictionaryEnumerator
Dim oDictionaryEntry As System.Collections.DictionaryEntry
Dim sKey As String
Dim sKeyPrefix As String = Me.Session.SessionID & "."
'Dim sPrefixes() As String = {Me.Session.SessionID &
".Query.", Me.Session.SessionID & ".QueryPreview"}
'Dim sSuffixes() As String = {".DataSet", ".DataView"}
'// EXCEPTION: Object reference not set to an object
'// This occurs because Me.Context is Nothing!
'// How do I get to the Cache from here?
oIEnumerator = Me.Context.Cache.GetEnumerator
While (oIEnumerator.MoveNext)
oDictionaryEntry = CType(oIEnumerator.Current,
System.Collections.DictionaryEntry)
sKey = oDictionaryEntry.Key.ToString()
If (sKey.StartsWith(sKeyPrefix)) Then
Call Context.Cache.Remove(sKey)
End If
End While
Catch oException As System.Exception
'** Do nothing
Stop
End Try
End Sub
How can I get to the Cache from the Session_End() event and remove
those things I need to remove?
Thanks!
Teemu Keiski - 02 Dec 2003 10:18 GMT
Hi,
ending the session might happen (and most probably does happen) outside any
web context i.e it is not tied to any particular request which again has
associated HttpContext. This means HttpContext is not necessarily available
on Session_End
You can access the cache statically via:
System.Web.HttpRuntime.Cache

Signature
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
> [VB.NET]
>
[quoted text clipped - 7 lines]
> I know how to iterate the Cache, but the Context object is Nothing
> when the Session_End() event fires! Take a look at my code:
'-------------------------------------------------------------------------
> ' Session_End()
'-------------------------------------------------------------------------
> Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
>
[quoted text clipped - 35 lines]
>
> Thanks!