简体   繁体   English

ASP.NET:这个在线用户计数器出了什么问题?

[英]ASP.NET: what's wrong with this online users counter?

I'm trying to count the amount of online users. 我正在尝试计算在线用户的数量。

This is the code: 这是代码:

protected void Application_Start()
{
    ...
    Application["OnlineUsers"] = 0;
}

private void Session_Start(object sender, EventArgs e)
{
            Application.Lock();
            Session["O"] = "OO"; // Need to have something in the session
            Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
            Application.UnLock();
}


private void Session_End(object sender, EventArgs e)
{
            Application.Lock();
            Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
            Application.UnLock();
}

There are <b>@Context.ApplicationInstance.Application["OnlineUsers"].ToString()</b> users online

It kind of works, but I always have the value that is greater than the actual amount of users online. 它有点工作,但我的值总是大于在线用户的实际数量。

And even worse: in Opera refreshing the page N times increases the amount of online users by N! 更糟糕的是:在Opera中刷新页面N次会增加N的在线用户数量!

It is important to note here that the ASP.NET is trying to be extremely efficient storing sessions for users. 这里需要注意的是, ASP.NET正在努力为用户存储sessions If ASP.NET doesn't have a reason to remember who you are, it won't . 如果ASP.NET没有理由记住你是谁,那就不会

When we request a page first time, a session object will be created and its session identifier will be sent to web-browser so browser can store session identifier in cookie (for identiity of request). 当我们第一次请求页面时,将创建会话对象并将其会话标识符发送到Web浏览器,以便浏览器可以在cookie中存储会话标识符(用于请求的标识)。 If that page is again submitted/postedback then the same session identifier from the cooike will be available to the app-server and it assume that this is an old-request. 如果再次提交/回发该页面,那么来自cooike的相同会话标识符将可用于app-server,并且它假定这是旧请求。 But in your case (you are refereshing a page), it means web-browser issue a fresh request (and of-course the request type will be GET) without sending cookies. 但在您的情况下(您正在引用页面),这意味着Web浏览器会发出新请求(当然请求类型将是GET)而不发送cookie。 So, it is better to redirect the user to a specific page on first request. 因此,最好在第一次请求时将用户重定向到特定页面。

The Session_End event handle will be called when Session get timeout (default value is 20 minutes) even after that client (browser) is closed (or ends the session). 即使客户端(浏览器)关闭(或结束会话),Session会在超时(默认值为20分钟)时调用Session_End事件句柄。

I know its not quite what you are asking but you can query the PerformanceCounter on IIS for this info 我知道它不是你要问的但你可以在IIS上查询PerformanceCounter以获取此信息

(Razor Example) (剃刀示例)

@using System.Diagnostics
@{
  var perf = new PerformanceCounter("ASP.NET", "State Server Sessions Active"); 
}
<h2>About</h2>
<p>
   @perf.NextValue()
</p>

I didn't check but your access to this might need a windows/service account in your app pool. 我没有检查,但您对此的访问权限可能需要您的应用程序池中的Windows /服务帐户。

You can also Increment and Decrement you own Performance counters and make them available to system admins via the tools they use to monitor Websites etc. 您还可以增加和减少自己的性能计数器,并通过用于监视网站等的工具将它们提供给系统管理员。

The SqlMembershipProvider has a facility for counting logged on users, which would mean you probably already have the data sitting in your database if you are using it to manage your forms authentication. SqlMembershipProvider具有计数登录用户的工具,这意味着如果您使用它来管理表单身份验证,您可能已经拥有数据。

You could also consider having your pages emit an ajax pulse every 'period of time' and count that. 你也可以考虑让你的页面每隔一段时间发出一个ajax脉冲并计算一下。 or have some applet, silverlight, flash etc doing the same. 或者有一些applet,silverlight,flash等做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM