HttpApplication inaccessible in Session_OnEnd:System.NullReferenceException

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!)
 
S

Steve Donnelly

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

John Saunders

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.
 
M

Manfred Braun

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!)

.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top