473,769 Members | 5,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global.asax: Session_End() Behavior

Hi. I am trying to maintain a list of people who are currently "online" in
SQL. I do this by adding a simple entry to a simple PeopleOnline table
whenever someone logs in to my site.

If they manually log OUT of the site, I have no problem deleting them from
the PeopleOnline table. But if they just close the browser, I was assuming
I'd have to use the Session_End() event in Global.asax even though I know
that this will only occur once the user's session has timed out.

But I have two questions about this:

1. When this even occurs, can I still access that user's Session? In other
words, inside the Session_End() function, can I reference the Session[]
object?

2. During development from VS2005, how do I test this? The problem is that
when I close the broswer that I use from within VS, everything sort of dies
so I don't think that the Session_End() ever occurs. How can I debug?

Thanks.

Alex
Jun 2 '07 #1
12 10698
More about this - I *have* figure out that if I set the timeout to something
short. But I still don't know why objects in the session seem to be gone by
the time Session_End occurs.

"Alex Maghen" wrote:
Hi. I am trying to maintain a list of people who are currently "online" in
SQL. I do this by adding a simple entry to a simple PeopleOnline table
whenever someone logs in to my site.

If they manually log OUT of the site, I have no problem deleting them from
the PeopleOnline table. But if they just close the browser, I was assuming
I'd have to use the Session_End() event in Global.asax even though I know
that this will only occur once the user's session has timed out.

But I have two questions about this:

1. When this even occurs, can I still access that user's Session? In other
words, inside the Session_End() function, can I reference the Session[]
object?

2. During development from VS2005, how do I test this? The problem is that
when I close the broswer that I use from within VS, everything sort of dies
so I don't think that the Session_End() ever occurs. How can I debug?

Thanks.

Alex
Jun 2 '07 #2
re:
!I still don't know why objects in the session seem to be gone
!by the time Session_End occurs.

When the Session_End event fires it's because the session is already gone,
either due to a session timeout or to Session.Abandon having been called in code.

ASP.NET would need to be psychic in order to have a "Pre_Session_En d" event.

The only built-in objects available from within the
Session_End event handler are Server and Application.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
=============== =============== ========
"Alex Maghen" <Al********@new sgroup.nospamwr ote in message
news:47******** *************** ***********@mic rosoft.com...
More about this - I *have* figure out that if I set the timeout to something
short. But I still don't know why objects in the session seem to be gone by
the time Session_End occurs.

"Alex Maghen" wrote:
>Hi. I am trying to maintain a list of people who are currently "online" in
SQL. I do this by adding a simple entry to a simple PeopleOnline table
whenever someone logs in to my site.

If they manually log OUT of the site, I have no problem deleting them from
the PeopleOnline table. But if they just close the browser, I was assuming
I'd have to use the Session_End() event in Global.asax even though I know
that this will only occur once the user's session has timed out.

But I have two questions about this:

1. When this even occurs, can I still access that user's Session? In other
words, inside the Session_End() function, can I reference the Session[]
object?

2. During development from VS2005, how do I test this? The problem is that
when I close the broswer that I use from within VS, everything sort of dies
so I don't think that the Session_End() ever occurs. How can I debug?

Thanks.

Alex


Jun 2 '07 #3
Well, I guess what I don't get about that is that, in fact, it is ASP that is
killing the Session at this point (because of timeout). So therefore it
shouldn't have to be "psychic" to implement a "Pre-end-Session." Further, I
have experimented a little and, in fact, I *can* get objects back out of the
Session within the Global.asax Session_End() event. There are some strange
differences, but simple objects in Session[] seem to be reliably available.

Alex

"Juan T. Llibre" wrote:
re:
!I still don't know why objects in the session seem to be gone
!by the time Session_End occurs.

When the Session_End event fires it's because the session is already gone,
either due to a session timeout or to Session.Abandon having been called in code.

ASP.NET would need to be psychic in order to have a "Pre_Session_En d" event.

The only built-in objects available from within the
Session_End event handler are Server and Application.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
=============== =============== ========
"Alex Maghen" <Al********@new sgroup.nospamwr ote in message
news:47******** *************** ***********@mic rosoft.com...
More about this - I *have* figure out that if I set the timeout to something
short. But I still don't know why objects in the session seem to be gone by
the time Session_End occurs.

"Alex Maghen" wrote:
Hi. I am trying to maintain a list of people who are currently "online" in
SQL. I do this by adding a simple entry to a simple PeopleOnline table
whenever someone logs in to my site.

If they manually log OUT of the site, I have no problem deleting them from
the PeopleOnline table. But if they just close the browser, I was assuming
I'd have to use the Session_End() event in Global.asax even though I know
that this will only occur once the user's session has timed out.

But I have two questions about this:

1. When this even occurs, can I still access that user's Session? In other
words, inside the Session_End() function, can I reference the Session[]
object?

2. During development from VS2005, how do I test this? The problem is that
when I close the broswer that I use from within VS, everything sort of dies
so I don't think that the Session_End() ever occurs. How can I debug?

Thanks.

Alex

Jun 2 '07 #4
re:
!I *can* get objects back out of the Session
!within the Global.asax Session_End() event.

Indeed, you can. The Session doesn't *end* until the event has finished executing.

The limitation I noted is for the *built-in* objects, i.e.,
Request, Response, Server, Application, and Session.

Out of those, the only ones available from within the
Session_End event handler are Server and Application.

While you could save an existing session variable to a log, by using
Server.MapPath, for example, you can't set a new session variable.

You can't response.write nor use context.current , either.
There's a lot you *can* do, however.

You need to remember, though, that Session_End may not fire under some circumstances,
so its use may not be as reliable as doing cleanup and logging when it *does* fire.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
=============== =============== ========
"Alex Maghen" <Al********@new sgroup.nospamwr ote in message
news:5A******** *************** ***********@mic rosoft.com...
Well, I guess what I don't get about that is that, in fact, it is ASP that is
killing the Session at this point (because of timeout). So therefore it
shouldn't have to be "psychic" to implement a "Pre-end-Session." Further, I
have experimented a little and, in fact, I *can* get objects back out of the
Session within the Global.asax Session_End() event. There are some strange
differences, but simple objects in Session[] seem to be reliably available.
Alex
"Juan T. Llibre" wrote:
>re:
!I still don't know why objects in the session seem to be gone
!by the time Session_End occurs.

When the Session_End event fires it's because the session is already gone,
either due to a session timeout or to Session.Abandon having been called in code.

ASP.NET would need to be psychic in order to have a "Pre_Session_En d" event.

The only built-in objects available from within the
Session_End event handler are Server and Application.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
============== =============== =========
"Alex Maghen" <Al********@new sgroup.nospamwr ote in message
news:47******* *************** ************@mi crosoft.com...
More about this - I *have* figure out that if I set the timeout to something
short. But I still don't know why objects in the session seem to be gone by
the time Session_End occurs.

"Alex Maghen" wrote:

Hi. I am trying to maintain a list of people who are currently "online" in
SQL. I do this by adding a simple entry to a simple PeopleOnline table
whenever someone logs in to my site.

If they manually log OUT of the site, I have no problem deleting them from
the PeopleOnline table. But if they just close the browser, I was assuming
I'd have to use the Session_End() event in Global.asax even though I know
that this will only occur once the user's session has timed out.

But I have two questions about this:

1. When this even occurs, can I still access that user's Session? In other
words, inside the Session_End() function, can I reference the Session[]
object?

2. During development from VS2005, how do I test this? The problem is that
when I close the broswer that I use from within VS, everything sort of dies
so I don't think that the Session_End() ever occurs. How can I debug?

Thanks.

Alex



Jun 2 '07 #5
Juan T. Llibre wrote:
re:
!I *can* get objects back out of the Session
!within the Global.asax Session_End() event.

Indeed, you can. The Session doesn't *end* until the event has finished executing.

The limitation I noted is for the *built-in* objects, i.e.,
Request, Response, Server, Application, and Session.

Out of those, the only ones available from within the
Session_End event handler are Server and Application.

While you could save an existing session variable to a log, by using
Server.MapPath, for example,
You can't use Server.MapPath in Session_End, as there is no page to make
the path relative to.
you can't set a new session variable.
Of course you can. It will only live during the execution of the
Session_End method, though, as the Session object goes away after that.
>
You can't response.write nor use context.current , either.
There's a lot you *can* do, however.

You need to remember, though, that Session_End may not fire under some circumstances,
so its use may not be as reliable as doing cleanup and logging when it *does* fire.

--
Göran Andersson
_____
http://www.guffa.com
Jun 3 '07 #6
re:
!You can't use Server.MapPath in Session_End, as there is no page to make the path relative to.

Server.MapPath works in the Session_Start event.

Retrieve the path in Session_Start, store it in a variable,
and then use this variable in the Session_End event.

The variable has to be declared as Shared.

re:
!>you can't set a new session variable.
!Of course you can. It will only live during the execution of the
!Session_End method, though, as the Session object goes away after that.

Which means...you can't use it, doesn't it ?

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
=============== =============== ========
"Göran Andersson" <gu***@guffa.co mwrote in message news:et******** *****@TK2MSFTNG P06.phx.gbl...
Juan T. Llibre wrote:
>re:
!I *can* get objects back out of the Session
!within the Global.asax Session_End() event.

Indeed, you can. The Session doesn't *end* until the event has finished executing.

The limitation I noted is for the *built-in* objects, i.e.,
Request, Response, Server, Application, and Session.

Out of those, the only ones available from within the
Session_End event handler are Server and Application.

While you could save an existing session variable to a log, by using
Server.MapPath , for example,
You can't use Server.MapPath in Session_End, as there is no page to make the path relative to.
>you can't set a new session variable.

Of course you can. It will only live during the execution of the Session_End method, though, as
the Session object goes away after that.
>>
You can't response.write nor use context.current , either.
There's a lot you *can* do, however.

You need to remember, though, that Session_End may not fire under some circumstances,
so its use may not be as reliable as doing cleanup and logging when it *does* fire.
-
Göran Andersson


Jun 3 '07 #7
Juan T. Llibre wrote:
re:
!>you can't set a new session variable.
!Of course you can. It will only live during the execution of the
!Session_End method, though, as the Session object goes away after that.

Which means...you can't use it, doesn't it ?
No, it doesn't. It means that you can only use it as long as it exists,
which is the duration of the call to the Session_End method.

There is hardly any reason to create any session variables at that
stage, but there is nothing that prevents it.

--
Göran Andersson
_____
http://www.guffa.com
Jun 3 '07 #8
re:
!There is hardly any reason to create any session variables at that stage

You mean : "there is *no* reason to create any session variables at that stage",
primarily because you can't *do anything* with a new session variable at that stage.

re:
!there is nothing that prevents it

Have you actually tried to do something useful with a new
session variable you have succeed in creating at that stage ?

If you can provide an example, maybe I'll have an easier time understanding your point.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
=============== =============== ========
"Göran Andersson" <gu***@guffa.co mwrote in message news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
Juan T. Llibre wrote:
>re:
!>you can't set a new session variable.
!Of course you can. It will only live during the execution of the
!Session_End method, though, as the Session object goes away after that.

Which means...you can't use it, doesn't it ?

No, it doesn't. It means that you can only use it as long as it exists, which is the duration of
the call to the Session_End method.

There is hardly any reason to create any session variables at that stage, but there is nothing
that prevents it.

--
Göran Andersson
_____
http://www.guffa.com

Jun 3 '07 #9
Hi Alex,

If you want to do is maintain a online user table, are those users
authenticated user(your application is secured through forms authentication
or...)? If the users you want to count are forms authenticated user, you
can use membership provider's support to get online user number. It use a
database field to track the last activity time of each user and calculate
online users based on this.

If the users are simply based on Session, I also suggest you create a
global online user table(use sessionID as key) to track all the online
users. And in addition to session_end event, you'd better also use a
background timer(thread) to constantly check the last visit timestamp of
each table entry, this entry can be updated each time the user visit the
site. And the entry will be removed in ether of the following cases:

1. session_end event fire for the certain session

2. background timer detect that the timestamp hasn't been updated for a
certain period.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
515
by: Phil Lamey | last post by:
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? ------------
1
4968
by: Steve | last post by:
I am trying to call a file delete function from inside Session_End in Global.asax.cs. However, everytime I step into my delete function from Session_End it jumps to the catch statement and I get the "Object reference not set to an instance of an object." I don't understand why it is happening. I would appreciate any help, thanks. Global.asax.cs using TheFile = Namespace.TheFile; private TheFile sessionEnd = new TheFile();
10
6769
by: David P. Donahue | last post by:
When I wrote websites in VB .NET, I would often put functions in Global for all the pages to call. Now, in C#, doing so results in "references to non-static objects" and whatnot. I realize what that means and all, but what I'm wondering is what's the best way around it? Say, for example, I want a function that takes a username and a password and returns true or false if it's a successful login, and I want any page or usercontrol in the...
4
11963
by: Max | last post by:
I've noticed some procedures don't run in the global.asax when you'd expect them to. I've rebuilt and set break points, but Application_Start just isn't firing today. Is there some configuration that prevents this from happening? I've never gotten Session_End to fire either. -M
2
4733
by: Bela | last post by:
Hello I was wondering if someone could help me out with a Session_End problem in my Global.asax. I've tried everything, and still no success Here is the scenario: sessionstate is set to InProc. I have timeout set to 1 min I have a variable in session_start that is incremented each new session, and it is then decremented in Session_End. I use this variable on a web form to show current users online. All of this works just fine, so I...
4
9181
by: Kim Bach Petersen | last post by:
I would like to record user behavior data stored in session variables. Since the data is modified throughout each session it seemed obvious to store the data when the session terminates - using Session_End in global.asax. Problem is, apparently the session-object terminating cannot be accessed from Session_End in global.asax!? What's the meaning of Session_End if you don't know which session is
9
2390
by: tshad | last post by:
I have an example I copied from "programming asp.net" (o'reilly) and can't seem to get the Sub (writefile) to execute. It displays all the response.write lines that are called directly, but not any of the response.write lines from inside the sub. ******************************************* <%@ Application Language="VB" %> <script runat="server">
7
6249
by: teo | last post by:
I need to use the 'Server.MapPath' function in the 'Session_End' event of the Global.asax file (to reach a folder and the clean some temporary files up), but it doesn't work: Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) Dim myPath As String = (Server.MapPath("../public/MyFolder/") End Sub
3
8650
by: teo | last post by:
I have two .aspx pages they use two different Global.asax files when I "Publish" the first .aspx page, its Global.asax is transformed in App_global.asax.compiled App_global.asax.dll to put in the Bin folder
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.