By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

Counting Sessions

P: n/a
kpg
Hi all,

Ah yes, the age old counting session question.

sessionstate
mode="InProc"
timeout="5"

Sub Application_Start()
Application("ConcurrentUsers") = 0
End Sub

Sub Session_Start()
Application.Lock()
Application("ConcurrentUsers") += 1
Application.UnLock()
End Sub

Sub Session_End()
Application.Lock()
Application("ConcurrentUsers") = -= 1
Application.UnLock()
End Sub

All's well, At first arrival of the login page Trace shows
ConcurrentUsers = 1

After login I navigate to a page that displays the count.
Still OK, trace and a label shows ConcurrentUsers = 1.

Private Sub Page_Load()
lblUsers.Text = Application("ConcurrentUsers")
End Sub

I have an "Update" button which does nothing but causes a
post back to re-display the page.

Private Sub btnUpdate_Click()
'no code here
End Sub

Trace and txtUsers now shows session count = 0! The session_end
event has not fired yet the Application variable is reset to 0.

My thoughts are that this must be a known issue or I'm just
missing something very basic.

Aug 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Take a look at the System.Diagnostics namespace's Performance counters

If you create a counter and use that to keep track of everything, you can go
ahead and monitor everything from aotside of your application's context.

"kpg" wrote:
Hi all,

Ah yes, the age old counting session question.

sessionstate
mode="InProc"
timeout="5"

Sub Application_Start()
Application("ConcurrentUsers") = 0
End Sub

Sub Session_Start()
Application.Lock()
Application("ConcurrentUsers") += 1
Application.UnLock()
End Sub

Sub Session_End()
Application.Lock()
Application("ConcurrentUsers") = -= 1
Application.UnLock()
End Sub

All's well, At first arrival of the login page Trace shows
ConcurrentUsers = 1

After login I navigate to a page that displays the count.
Still OK, trace and a label shows ConcurrentUsers = 1.

Private Sub Page_Load()
lblUsers.Text = Application("ConcurrentUsers")
End Sub

I have an "Update" button which does nothing but causes a
post back to re-display the page.

Private Sub btnUpdate_Click()
'no code here
End Sub

Trace and txtUsers now shows session count = 0! The session_end
event has not fired yet the Application variable is reset to 0.

My thoughts are that this must be a known issue or I'm just
missing something very basic.

Aug 17 '06 #2

P: n/a
kpg
As =?Utf-8?B?RGF2aWQgSmVzc2Vl?= once said in
microsoft.public.dotnet.framework.aspnet
Take a look at the System.Diagnostics namespace's Performance counters

If you create a counter and use that to keep track of everything, you
can go ahead and monitor everything from aotside of your application's
context.
Great idea! I tried to create my own counter but found I needed
security rights to the registry, so I decided to simply read the
value in the "ASP.NET Applications", "Sessions Active" counter for
my application instance. This is all I ever wanted anyway.

Here's the code:

Dim objCounter As New System.Diagnostics.PerformanceCounter( _
"ASP.NET Applications",
"Sessions Active",
"_LM_w3svc_1_root_LSFB1")

lblUsers.Text = objCounter.RawValue.ToString

But I was shocked to find that this technique had the same exact
problem as my before. After hitting the update button the Active
Session count went to 0!

This, of course, prompted me to look very closey at my code.

In the page_load event I have:

If Not Page.IsPostBack Then
Session.Timeout = 20
End If

If I remove the Session.Timeout statement both techniques work
fine and give the same results - an accurate count of active
sessions.

I much prefer the performance counter technique and think it's
quite superior to the Application varailbe technique, I am
comfused as to why resetting the session timeout removes the
session, even from the NT perfromance counter who's job it is to
count sessions?

The session must be there, because session level variable retain
their value, in fact even (other) Application level varaibles
retian their value.

What's going on here?

BTW - I have a reason to set the session timeout, as I want
different users to have different timeouts. Perhaps there is
a more appropriate palce to change the session timeout value
to accomplish this that does not screw up the session count.
Aug 17 '06 #3

P: n/a
I decompiles the System.Web.SessionState.HttpSessionStateContainer to see
what really happens and couldn't find anything that would cause screwy
behavior with the Session, so I can't help ya there. I get the feeling,
however, that the timeout does not take effect until the page processing is
complete (that's an intuitive guess, so don't go to the bank on it). If
there any way you can place that code somewhere outside of the page? maybe
the global authenticated event?
"kpg" wrote:
As =?Utf-8?B?RGF2aWQgSmVzc2Vl?= once said in
microsoft.public.dotnet.framework.aspnet
Take a look at the System.Diagnostics namespace's Performance counters

If you create a counter and use that to keep track of everything, you
can go ahead and monitor everything from aotside of your application's
context.

Great idea! I tried to create my own counter but found I needed
security rights to the registry, so I decided to simply read the
value in the "ASP.NET Applications", "Sessions Active" counter for
my application instance. This is all I ever wanted anyway.

Here's the code:

Dim objCounter As New System.Diagnostics.PerformanceCounter( _
"ASP.NET Applications",
"Sessions Active",
"_LM_w3svc_1_root_LSFB1")

lblUsers.Text = objCounter.RawValue.ToString

But I was shocked to find that this technique had the same exact
problem as my before. After hitting the update button the Active
Session count went to 0!

This, of course, prompted me to look very closey at my code.

In the page_load event I have:

If Not Page.IsPostBack Then
Session.Timeout = 20
End If

If I remove the Session.Timeout statement both techniques work
fine and give the same results - an accurate count of active
sessions.

I much prefer the performance counter technique and think it's
quite superior to the Application varailbe technique, I am
comfused as to why resetting the session timeout removes the
session, even from the NT perfromance counter who's job it is to
count sessions?

The session must be there, because session level variable retain
their value, in fact even (other) Application level varaibles
retian their value.

What's going on here?

BTW - I have a reason to set the session timeout, as I want
different users to have different timeouts. Perhaps there is
a more appropriate palce to change the session timeout value
to accomplish this that does not screw up the session count.
Aug 17 '06 #4

P: n/a
kpg
As =?Utf-8?B?RGF2aWQgSmVzc2Vl?= once said in
microsoft.public.dotnet.framework.aspnet
I decompiles the System.Web.SessionState.HttpSessionStateContainer to
see what really happens and couldn't find anything that would cause
screwy behavior with the Session, so I can't help ya there. I get the
feeling, however, that the timeout does not take effect until the page
processing is complete (that's an intuitive guess, so don't go to the
bank on it). If there any way you can place that code somewhere
outside of the page? maybe the global authenticated event?
hmmmm...well I moved it to the previous page, the one that redirects,
to this page, and the session counter still gets zapped. I can watch
it using the performance console, as soon as "session.timeout = 20" is
executed the performance counter goes to 0 (from 1). If I have several
sessions active it will just decrement the counter.

Interestingly, let's assume the session has been abandoned (which IMO
it has not) then once I re-hit the server with the postback I would
expect it to be re-created - it is not. This tell me that the session
is really still there (which I know is the case).

This behavior is exhibited on both my development machine (WinXP Pro)
and the deployment server (Win2k).

It's not a big issue, just worrisome.

In the final product I won't be using session state anyway, but I would
still like an accurate (as possible) count of active sessions.

Aug 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.