Global.asax Session_OnEnd not firing

G

Guest

Hi All,

I have the following code but for some reason I cannot get the Session_OnEnd
event to fire.

I am trying to limit the amount of connections a browser session can have.
Where the application is a virtual directory.

Any ideas?
------------
default.aspx.vb

Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents lblSessionCount As System.Web.UI.WebControls.Label
Protected WithEvents lblTotalCount As System.Web.UI.WebControls.Label
Protected WithEvents lblMSG As System.Web.UI.WebControls.Label

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
Dim SessionCount As Integer
Dim SessionID As Integer
SessionCount = Utility.GetSessionCount(Server.MapPath("/GDAM"),
Session.SessionID)
lblSessionCount.Text = SessionCount
Dim TotalCount As Integer
TotalCount = Utility.GetTotalCount(Server.MapPath("/GDAM"))
lblTotalCount.Text = TotalCount

If SessionCount < 2 Then
If TotalCount < 6 Then
Utility.IncrementSessionCount(Server.MapPath("/GDAM"),
Session.SessionID)
GetGDAM()
Else
lblMSG.Text = "Sorry all the connections are currently
busy: " & TotalCount
End If
Else
lblMSG.Text = "Sorry too many connections for this session:
" & SessionCount
End If
End If

End Sub

Sub GetGDAM()
lblMSG.Text = "Went to get GDAM"
Session.Abandon()
End Sub

End Class
------------
sessions.xml

<SESSIONS>
<SESSION>
<ID>1</ID>
<COUNT>2</COUNT>
</SESSION>
<SESSION>
<ID>2</ID>
<COUNT>0</COUNT>
</SESSION>
</SESSIONS>

-----

Global.asax

Public Class Global
Inherits System.Web.HttpApplication

#Region " Component Designer Generated Code "

Public Sub New()
MyBase.New()

'This call is required by the Component Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()
End Sub

#End Region

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
Application("Start") = Now()
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
Session.Timeout = 5
Session("Start") = Now()
Utility.NewSession(Server.MapPath("/GDAM"), Session.SessionID)
End Sub

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
' Fires at the beginning of each request
End Sub

Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
' Fires upon attempting to authenticate the use
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an error occurs
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session ends
Utility.KillSession(Server.MapPath("/GDAM"), Session.SessionID)
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application ends
End Sub

End Class

-------------
Utility.vb

Public Class Utility

Public Shared Function GetSessionCount(ByVal AppPath As String, ByVal
SessionID As String) As Integer
'''''Get Session XML'''''
Dim xmlSessions As System.Xml.XmlDocument = New
System.Xml.XmlDocument()
xmlSessions.Load(AppPath & "\sessions.xml")
Dim lstSessions As System.Xml.XmlNodeList
Dim ndSession As System.Xml.XmlNode

'''''Get the Count for this sessionid
lstSessions = xmlSessions.SelectNodes("SESSIONS/SESSION")
For Each ndSession In lstSessions
If ndSession.SelectSingleNode("ID").InnerText = SessionID Then
Return ndSession.SelectSingleNode("COUNT").InnerText
End If
Next
End Function

Public Shared Function GetTotalCount(ByVal AppPath As String) As Integer
'''''Get Session XML'''''
Dim TotalCount As Integer
TotalCount = 0
Dim xmlSessions As System.Xml.XmlDocument = New
System.Xml.XmlDocument()
xmlSessions.Load(AppPath & "\sessions.xml")
Dim lstSessions As System.Xml.XmlNodeList
Dim ndSession As System.Xml.XmlNode

'''''Total up the Session Counts
lstSessions = xmlSessions.SelectNodes("SESSIONS/SESSION")
For Each ndSession In lstSessions
TotalCount = TotalCount +
ndSession.SelectSingleNode("COUNT").InnerText
Next

Return TotalCount
End Function

Public Shared Sub IncrementSessionCount(ByVal AppPath As String, ByVal
SessionID As String)
'''''Get Session XML'''''
Dim xmlSessions As System.Xml.XmlDocument = New
System.Xml.XmlDocument()
xmlSessions.Load(AppPath & "\sessions.xml")
Dim lstSessions As System.Xml.XmlNodeList
Dim ndSession As System.Xml.XmlNode
Dim SessionCount As Integer
Dim NewSessionCount As Integer

'''''Get the Count for this sessionid
lstSessions = xmlSessions.SelectNodes("SESSIONS/SESSION")
For Each ndSession In lstSessions
If ndSession.SelectSingleNode("ID").InnerText = SessionID Then
SessionCount = ndSession.SelectSingleNode("COUNT").InnerText
NewSessionCount = SessionCount + 1
ndSession.SelectSingleNode("COUNT").InnerText =
NewSessionCount
End If
Next
xmlSessions.Save(AppPath & "\sessions.xml")
End Sub

Public Shared Sub NewSession(ByVal AppPath As String, ByVal SessionID As
String)
'''''Get Session XML'''''
Dim xmlSessions As System.Xml.XmlDocument = New
System.Xml.XmlDocument()
xmlSessions.Load(AppPath & "\sessions.xml")
'Create New Child Nodes
Dim eleSess As System.Xml.XmlElement =
xmlSessions.CreateElement("SESSION")
Dim eleID As System.Xml.XmlElement = xmlSessions.CreateElement("ID")
Dim eleCount As System.Xml.XmlElement =
xmlSessions.CreateElement("COUNT")

'Set the text
Dim eleIDText As System.Xml.XmlText =
xmlSessions.CreateTextNode(SessionID)
Dim eleCountText As System.Xml.XmlText = xmlSessions.CreateTextNode(0)

'Append parentnode to root
xmlSessions.DocumentElement.PrependChild(eleSess)

'append the nodes to the parentNode without the value
eleSess.AppendChild(eleID)
eleSess.AppendChild(eleCount)

'Save the text values to the nodes
eleID.AppendChild(eleIDText)
eleCount.AppendChild(eleCountText)
xmlSessions.Save(AppPath & "\sessions.xml")
End Sub

Public Shared Sub KillSession(ByVal AppPath As String, ByVal SessionID
As String)
'''''Get Session XML'''''
Dim xmlSessions As System.Xml.XmlDocument = New
System.Xml.XmlDocument()
xmlSessions.Load(AppPath & "\sessions.xml")
'Create New Child Nodes
Dim eleSess As System.Xml.XmlElement =
xmlSessions.CreateElement("SESSION")
Dim eleID As System.Xml.XmlElement = xmlSessions.CreateElement("ID")
Dim eleCount As System.Xml.XmlElement =
xmlSessions.CreateElement("COUNT")

'Set the text
Dim eleIDText As System.Xml.XmlText =
xmlSessions.CreateTextNode(SessionID)
Dim eleCountText As System.Xml.XmlText = xmlSessions.CreateTextNode(0)

'Append parentnode to root
xmlSessions.DocumentElement.PrependChild(eleSess)

'append the nodes to the parentNode without the value
eleSess.AppendChild(eleID)
eleSess.AppendChild(eleCount)

'Save the text values to the nodes
eleID.AppendChild(eleIDText)
eleCount.AppendChild(eleCountText)
xmlSessions.Save(AppPath & "\sessions.xml")
End Sub

End Class

------------

Any help/information is greatly appreciated.

Thank you,

Phil Lamey, EIT
CGI Consultant
 
M

Marina

What do you expect to be causing this event?

This even is fired when the session is explicitly abandoned in code, or when
the session timeout has been reached due to no requests from the client. It
will NOT be called as soon as the user navigates away from your site or
closes the browser.
 
G

Guest

I have been waiting for anything to fire the OnEnd code.
Timeout/Abandon/Navigation ... but nothing seems to make the OnEnd event
fire. I have a breakpoint in my code in the OnEnd section of the Global.asax
and it is never reached.
 
D

darin dimitrov

Phil,

As Marina said the Session_End event is fired only if the session
times out, is explicitly abandoned in code and I would add: if you are
using the *InProc* session state mode. What session state mode do you
use? To debug the Session_End event you could set the timeout in your
web.config to say 1 minute.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top