0

i' m developing webapp in ASP.NET 2.0 (C#). I've problem to resolve issue.

The application should show users on-line, and only for administrator should show the user name. I'm using Application[] object to store usermame and count, setting the value in Globall.asax file.

In the following code i'll show the section relative to counter:

   protected void Application_Start(object sender, EventArgs e){
            Application["OnlineCounter"] = 0; 
    }
    protected void Session_Start(Object sender, EventArgs e){
            // Code that runs when a new session is started
            if (Application["OnlineCounter"] != null){
                Application.Lock();
                Application["OnlineCounter"] = ((int)Application["OnlineCounter"]) + 1;
                Application.UnLock();
            }
    }
    protected void Session_End(Object sender, EventArgs e){
            // Code that runs when a new session is started
            if (Application["OnlineCounter"] != null){
                Application.Lock();
                Application["OnlineCounter"] = ((int)Application["OnlineCounter"]) - 1;
                Application.UnLock();
            }
    }

Using this code on my local machine i can count correctly the online user. Instead, when i publish this code on server (Windows 2003 Server and IIS6) i found the following problem: accessing from my machine with 3 different user (using different browser), i will see only 1 user in a single page (In each browser i see only 1 online user) !

There are some issue for this ? Any suggestion is appreciated. Thanx

arthur86
  • 531
  • 1
  • 3
  • 20
  • possible duplicate: http://stackoverflow.com/questions/10481351/counting-online-users-using-asp-net http://stackoverflow.com/questions/671129/what-actually-causes-session-start-to-be-called – Christopher Rathermel May 08 '12 at 09:30
  • Thanks Christoper, but i don't think my problem is related to session event. The Application variable (Application[]) is Initialized, the problem is that seem to be different from each session. – arthur86 May 08 '12 at 09:41

2 Answers2

1

You can use a performance counter to get that number.

Here you have the list of performance counters for ASP.NET. Look for the description of "Sessions Active" under "ASP.NET Application Performance Counters".

Then you can use PerformanceCounter class to get the value of that performance counter for you application.

The problem is that this requires privileges:

To read performance counters in Windows Vista, Windows XP Professional x64 Edition, or Windows Server 2003, you must either be a member of the Performance Monitor Users group or have administrative privileges.

One way to do this would be to impersonate a part of your application to be run by a user with those privileges. It could be an .asmx web service, or the web forms itself which will show the required info. You can impersonate only that service using location and identity impersonate in web.config. Set up a user with the needed privileges and impersonate that user.

BenG
  • 14,826
  • 5
  • 45
  • 60
JotaBe
  • 38,030
  • 8
  • 98
  • 117
0

Is the server configured to use multiple worker processes per application pool?

ASP.NET State Management Recommendations: http://msdn.microsoft.com/en-us/library/z1hkazw7.aspx

Application scope: The scope of application state can also be a disadvantage. Variables stored in application state are global only to the particular process the application is running in, and each application process can have different values. Therefore, you cannot rely on application state to store unique values or update global counters in Web-garden and Web-farm server configurations.

Configuring Web Gardens with IIS 6.0: https://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/659f2e2c-a58b-4770-833b-df96cabe569e.mspx?mfr=true

David Tischler
  • 2,642
  • 22
  • 13
  • I checked the "Maximunm number of worker processes" on IIS and that's setted to 4. I shound increase that ? – arthur86 May 08 '12 at 09:50
  • No. If your maximum number of worker processes is more than 1 then it can be that each request is handled by a different process, therefore each one gets a different instance of the Application object - it exists once per process, not once per web application. So if this is the cause of the problem, either decrease the number to 1, or store the counter somewhere else, like a database - but then you would have to keep in mind that sessions can end without the Session_End-event being executed, e.g. when a worker process gets killed. – David Tischler May 08 '12 at 10:05
  • I've tried to decrease the worker process to 1. It still show one user for each browser. – arthur86 May 08 '12 at 10:13
  • After this change, did you restart IIS? – David Tischler May 08 '12 at 10:43
  • I've recycle app pool, but i've not restart IIS because i can't stop the application. – arthur86 May 08 '12 at 11:41