HttpApplication inaccessible in Session_OnEnd:System.NullReferenceException

Discussion in 'ASP .Net' started by Manfred Braun, Aug 30, 2003.

  1. Hi All,

    In my [global.asa's], I will decrement a counter, like :

    protected void Session_End(Object sender, EventArgs e)
    {
    ActiveSessionCount--; //CRASH !!
    }

    But, unexpectedly, it give me an "Global_asax]System.NullReferenceException:
    Object reference not set to an instance of an object." at line [above] and
    line [below].
    I handle the increment/decrement of my session-counter with the code below:

    protected static int ActiveSessionCount
    {

    get {
    HttpContext.Current.Application.Lock(); //CRASH!!
    int i = activeSessionCount; // private static declared.
    HttpContext.Current.Application.UnLock();
    return i;
    }

    set {
    HttpContext.Current.Application.Lock();
    activeSessionCount = value;
    HttpContext.Current.Application.UnLock();
    }
    }

    Why can the "HttpContext.Current.Application.Lock();" return a
    NullReferenceException??? I am not able to understand this! Any help would
    really be very welcomed!!!!

    Best regards,
    Manfred Braun

    (Private)
    Mannheim
    Germany

    mailto:
    (Remove the anti-spam-underscore to mail me!)
    Manfred Braun, Aug 30, 2003
    #1
    1. Advertising

  2. HttpContext.Current is a Request context. There is no
    request during Session_End(), so HttpContext.Current is
    null.

    >-----Original Message-----
    >Hi All,
    >
    > In my [global.asa's], I will decrement a counter, like :
    >
    > protected void Session_End(Object sender, EventArgs e)
    > {
    > ActiveSessionCount--; //CRASH !!
    >}
    >
    >But, unexpectedly, it give me an "Global_asax]

    System.NullReferenceException:
    > Object reference not set to an instance of an object."

    at line [above] and
    >line [below].
    >I handle the increment/decrement of my session-counter

    with the code below:
    >
    > protected static int ActiveSessionCount
    > {
    >
    > get {
    > HttpContext.Current.Application.Lock(); //CRASH!!
    > int i = activeSessionCount; // private static

    declared.
    > HttpContext.Current.Application.UnLock();
    > return i;
    > }
    >
    > set {
    > HttpContext.Current.Application.Lock();
    > activeSessionCount = value;
    > HttpContext.Current.Application.UnLock();
    > }
    > }
    >
    >Why can the "HttpContext.Current.Application.Lock();"

    return a
    >NullReferenceException??? I am not able to understand

    this! Any help would
    >really be very welcomed!!!!
    >
    >Best regards,
    >Manfred Braun
    >
    >(Private)
    >Mannheim
    >Germany
    >
    >mailto:
    >(Remove the anti-spam-underscore to mail me!)
    >
    >.
    >
    Steve Donnelly, Aug 30, 2003
    #2
    1. Advertising

  3. "Manfred Braun" <> wrote in message
    news:u%...
    > Hello Steve,
    >
    > thanks so far, now I am more near to understand what I do ;-)
    > Do you know, how I can access my application-instance to lock it??
    >


    For what you're doing, a lock of the Application instance is overkill. Just
    use Interlocked.Decrement.
    --
    John Saunders
    Internet Engineer
    John Saunders, Aug 30, 2003
    #3
  4. Hi All,

    thanks a lot for all the help!!!! I have it working now; Was just
    disappointed how to lock the application itself from Session_End(), but
    anyway, I have it working!!!

    Thanks so far and
    best regards,
    Manfred

    "Sherif ElMetainy" <> wrote in message
    news:...
    > Hello
    >
    > you can use the c# lock keyword instead of Application.Lock
    > ex.
    >
    > private static readonly object sync = new object();
    >
    > protected static int ActiveSessionCount {
    > get {
    > int i;
    > lock(sync) {
    > i = activeSessionCount;
    > }
    > return i;
    > } set {
    > {
    > lock(sync)
    > {
    > activeSessionCount = value;
    > }
    > }
    > }
    >
    >
    >
    > "Manfred Braun" <> wrote in message
    > news:u#...
    > > Hello Steve,
    > >
    > > thanks so far, now I am more near to understand what I do ;-)
    > > Do you know, how I can access my application-instance to lock it??
    > >
    > > Thanks so far and
    > > best regards,
    > > Manfred
    > >
    > >
    > > "Steve Donnelly" <> wrote in message
    > > news:023701c36f3b$84df82e0$...
    > > > HttpContext.Current is a Request context. There is no
    > > > request during Session_End(), so HttpContext.Current is
    > > > null.
    > > >
    > > > >-----Original Message-----
    > > > >Hi All,
    > > > >
    > > > > In my [global.asa's], I will decrement a counter, like :
    > > > >
    > > > > protected void Session_End(Object sender, EventArgs e)
    > > > > {
    > > > > ActiveSessionCount--; //CRASH !!
    > > > >}
    > > > >
    > > > >But, unexpectedly, it give me an "Global_asax]
    > > > System.NullReferenceException:
    > > > > Object reference not set to an instance of an object."
    > > > at line [above] and
    > > > >line [below].
    > > > >I handle the increment/decrement of my session-counter
    > > > with the code below:
    > > > >
    > > > > protected static int ActiveSessionCount
    > > > > {
    > > > >
    > > > > get {
    > > > > HttpContext.Current.Application.Lock(); //CRASH!!
    > > > > int i = activeSessionCount; // private static
    > > > declared.
    > > > > HttpContext.Current.Application.UnLock();
    > > > > return i;
    > > > > }
    > > > >
    > > > > set {
    > > > > HttpContext.Current.Application.Lock();
    > > > > activeSessionCount = value;
    > > > > HttpContext.Current.Application.UnLock();
    > > > > }
    > > > > }
    > > > >
    > > > >Why can the "HttpContext.Current.Application.Lock();"
    > > > return a
    > > > >NullReferenceException??? I am not able to understand
    > > > this! Any help would
    > > > >really be very welcomed!!!!
    > > > >
    > > > >Best regards,
    > > > >Manfred Braun
    > > > >
    > > > >(Private)
    > > > >Mannheim
    > > > >Germany
    > > > >
    > > > >mailto:
    > > > >(Remove the anti-spam-underscore to mail me!)
    > > > >
    > > > >.
    > > > >

    > >

    >
    >
    Manfred Braun, Aug 30, 2003
    #4
    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. William
    Replies:
    0
    Views:
    1,586
    William
    Aug 10, 2004
  2. msnews.microsoft.com

    Help with System.NullReferenceException

    msnews.microsoft.com, Jul 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    560
    msnews.microsoft.com
    Jul 16, 2003
  3. Angel Faus
    Replies:
    2
    Views:
    977
    Jeff Washburn
    Dec 1, 2003
  4. PatricQ
    Replies:
    3
    Views:
    9,541
    PatricQ
    Feb 18, 2004
  5. Steven Cheng[MSFT]

    RE: System.NullReferenceException on trace.axd

    Steven Cheng[MSFT], Apr 20, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    539
    [MSFT]
    Apr 29, 2004
Loading...

Share This Page