请在下面尝试。
将以下代码放入项目的 Global.asax 文件中。
Shared Dict As New Dictionary(Of String, String)
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application startup
Application.Add("AllSession", Dict)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
If Dict.ContainsKey(Me.Session.SessionID) Then
HttpRuntime.Cache.Remove(Dict.Item(Me.Session.SessionID))
Dict.Remove(Me.Session.SessionID)
Application.Add("AllSession", Dict)
End If
End Sub
请将此代码放入您分配会话的 vb 文件中。
Dim cacheKey As New Random
Dim DictcacheKey As Dictionary(Of String, String)
DictcacheKey = Application("AllSession")
If Not DictcacheKey.ContainsKey(HttpContext.Current.Session.SessionID) Then
DictcacheKey.Add(HttpContext.Current.Session.SessionID, cacheKey.Next())
Application.Add("AllSession", DictcacheKey)
Cache.Insert(cacheKey.Next(), DictcacheKey, Nothing, DateTime.Today.AddDays(1), TimeSpan.Zero)
End If
注意:如果您不使用 vb,请在 C# 中转换此代码。