HttpApplication inaccessible in Session_OnEnd:System.NullReferenceException

  • Thread starter Thread starter Manfred Braun
  • Start date Start date
M

Manfred Braun

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:[email protected]
(Remove the anti-spam-underscore to mail me!)
 
HttpContext.Current is a Request context. There is no
request during Session_End(), so HttpContext.Current is
null.
 
Manfred Braun said:
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.
 
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 said:
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 said:
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 said:
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:[email protected]
(Remove the anti-spam-underscore to mail me!)

.
 
Back
Top