Global.asax: Session_End() Behavior

Discussion in 'ASP .Net' started by =?Utf-8?B?QWxleCBNYWdoZW4=?=, Jun 2, 2007.

  1. 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
     
    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Jun 2, 2007
    #1
    1. Advertising

  2. 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
     
    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Jun 2, 2007
    #2
    1. Advertising

  3. 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_End" 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" <> wrote in message
    news:...
    > 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
     
    Juan T. Llibre, Jun 2, 2007
    #3
  4. 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_End" 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" <> wrote in message
    > news:...
    > > 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

    >
    >
    >
    >
     
    =?Utf-8?B?QWxleCBNYWdoZW4=?=, Jun 2, 2007
    #4
  5. 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" <> wrote in message
    news:...
    > 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_End" 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" <> wrote in message
    >> news:...
    >> > 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

    >>
    >>
    >>
    >>
     
    Juan T. Llibre, Jun 2, 2007
    #5
  6. 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
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jun 3, 2007
    #6
  7. 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" <> wrote in message news:...
    > 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
     
    Juan T. Llibre, Jun 3, 2007
    #7
  8. 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
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jun 3, 2007
    #8
  9. 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" <> wrote in message news:%...
    > 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
     
    Juan T. Llibre, Jun 3, 2007
    #9
  10. 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.
     
    Steven Cheng[MSFT], Jun 4, 2007
    #10
  11. Juan T. Llibre wrote:
    > 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",


    No, I don't.

    > primarily because you can't *do anything* with a new session variable at that stage.


    Of course you can do something with a session variable. There is hardly
    any reason, though.

    > 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 ?


    No, I haven't. I don't need to try every single thing that I discuss in
    newsgroups to know how it works. Sometimes I even answer questions about
    things that I didn't even knew existed before I read the question...

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


    ShoppingCart.Remove(); // uses session["cartId"]
    session["cartId"] = session["secondaryCartId"];
    ShoppingCart.Remove();

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jun 4, 2007
    #11
  12. re:
    !> ShoppingCart.Remove(); // uses session["cartId"]
    !> session["cartId"] = session["secondaryCartId"];
    !> ShoppingCart.Remove();

    You want that code to run every time a session ends ?

    What will you do with session["cartId"] , now that you have created 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" <> wrote in message news:%...
    > Juan T. Llibre wrote:
    >> 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",

    >
    > No, I don't.
    >
    >> primarily because you can't *do anything* with a new session variable at that stage.

    >
    > Of course you can do something with a session variable. There is hardly any reason, though.
    >
    >> 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 ?

    >
    > No, I haven't. I don't need to try every single thing that I discuss in newsgroups to know how it
    > works. Sometimes I even answer questions about things that I didn't even knew existed before I
    > read the question...
    >
    >> If you can provide an example, maybe I'll have an easier time understanding your point.

    >
    > ShoppingCart.Remove(); // uses session["cartId"]
    > session["cartId"] = session["secondaryCartId"];
    > ShoppingCart.Remove();
    >
    > --
    > Göran Andersson
    > _____
    > http://www.guffa.com
     
    Juan T. Llibre, Jun 4, 2007
    #12
  13. Juan T. Llibre wrote:
    > re:
    > !> ShoppingCart.Remove(); // uses session["cartId"]
    > !> session["cartId"] = session["secondaryCartId"];
    > !> ShoppingCart.Remove();
    >
    > You want that code to run every time a session ends ?
    >
    > What will you do with session["cartId"] , now that you have created it ?


    You missed the comment in the code? Ok, will it be clearer if I rewrite
    the example like this?

    session["UsedByRemove"] = session["CartId1"];
    ShoppingCart.Remove();
    session["UsedByRemove"] = session["CartId2"];
    ShoppingCart.Remove();

    --
    Göran Andersson
    _____
    http://www.guffa.com
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Jun 4, 2007
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmVsYQ==?=

    Problem with Session_End in Global.asax

    =?Utf-8?B?QmVsYQ==?=, Apr 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    3,562
    coollzh
    Apr 13, 2004
  2. Samuel Stanojevic
    Replies:
    1
    Views:
    956
    Samuel Stanojevic
    Sep 15, 2004
  3. =?Utf-8?B?YmNoYXJsZXM=?=

    global.asax and global.asax.cs

    =?Utf-8?B?YmNoYXJsZXM=?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    864
    =?Utf-8?B?YmNoYXJsZXM=?=
    Oct 5, 2004
  4. Kim Bach Petersen
    Replies:
    5
    Views:
    15,813
    JamesJM
    Aug 3, 2011
  5. Mark Rae

    Global.asax / Global.asax.cs in v2

    Mark Rae, May 23, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    3,188
    Mark Rae
    May 23, 2006
Loading...

Share This Page