简体   繁体   English

HttpApplication class 的Application 属性可以用来计算当前活跃用户吗?

[英]Can Application property of HttpApplication class be used to calculate current Active Users?

I am using the following code in the ASP.NET web forms Global.aspx page to count the number of current active users.我在 ASP.NET web forms Global.aspx 页面中使用以下代码来计算当前活跃用户的数量。 It does work when I do local testing.当我进行本地测试时,它确实有效。

Understanding is that Application is an Application-level variable and can be accessed at the application level.理解是Application是Application级别的变量,可以在应用级别访问。 So value stored in one session will be available in other sessions.因此存储在一个 session 中的值将在其他会话中可用。

<%@ Application Language="C#" %>  
  
<script runat="server">  
  
    void Application_Start(object sender, EventArgs e)   
    {  
        // Code that runs on application startup  
        Application["TotalOnlineUsers"] = 0;  
    }  
      
    void Application_End(object sender, EventArgs e)   
    {  
        //  Code that runs on application shutdown  
  
    }  
          
    void Application_Error(object sender, EventArgs e)   
    {   
        // Code that runs when an unhandled error occurs  
  
    }  
  
    void Session_Start(object sender, EventArgs e)   
    {  
        // Code that runs when a new session is started  
        Application.Lock();  
        Application["TotalOnlineUsers"] = (int)Application["TotalOnlineUsers"] + 1;  
        Application.UnLock();  
    }  
  
    void Session_End(object sender, EventArgs e)   
    {  
        // Code that runs when a session ends.   
        // Note: The Session_End event is raised only when the sessionstate mode  
        // is set to InProc in the Web.config file. If session mode is set to StateServer   
        // or SQLServer, the event is not raised.  
        Application.Lock();  
        Application["TotalOnlineUsers"] = (int)Application["TotalOnlineUsers"] - 1;  
        Application.UnLock();  
    }  
         
</script>

But while going through this link https://docs.microsoft.com/en-us/dotnet/api/system.web.httpapplication?redirectedfrom=MSDN&view=netframework-4.8 , it says that member variables can be used to store per-request data .但是在通过这个链接https://docs.microsoft.com/en-us/dotnet/api/system.web.httpapplication?redirectedfrom=MSDN&view=netframework-4.8时,它说成员变量可用于存储 per-请求数据

Based on this statement and below complete paragraph, there can be multiple instances of HttpApplication for a single application, if not application will be super slow as one HttpApplication instance can process only one request at one point in time基于此声明和下面的完整段落,单个应用程序可以有多个 HttpApplication 实例,如果没有,应用程序将非常慢,因为一个 HttpApplication 实例在一个时间点只能处理一个请求

Because of this, each HttpApplication will have its own Application variable and the count will be saved at HttpApplication level.因此,每个 HttpApplication 都有自己的 Application 变量,并且计数将保存在 HttpApplication 级别。

Instances of the HttpApplication class are created in the ASP.NET infrastructure, not by the user directly. HttpApplication class 的实例是在 ASP.NET 基础架构中创建的,而不是由用户直接创建的。 One instance of the HttpApplication class is used to process many requests in its lifetime. HttpApplication class 的一个实例用于在其生命周期内处理许多请求。 However, it can process only one request at a time.但是,它一次只能处理一个请求。 Thus, member variables can be used to store per-request data.因此,成员变量可用于存储每个请求的数据。

An application raises events that can be handled by custom modules that implement the IHttpModule interface or by event handler code that is defined in the Global.asax file.应用程序引发的事件可由实现 IHttpModule 接口的自定义模块或由 Global.asax 文件中定义的事件处理程序代码处理。 Custom modules that implement the IHttpModule interface can be put in the App_Code folder or in a DLL in the Bin folder.实现 IHttpModule 接口的自定义模块可以放在 App_Code 文件夹中或 Bin 文件夹中的 DLL 中。

So, to get the exact count without any chance of miscalculating, should I use static variables?那么,为了得到准确的计数而不会有任何错误计算的机会,我应该使用 static 变量吗?

I believe your code will work correctly, assuming you're using InProc session state and aren't running in a Web farm.我相信您的代码将正常工作,假设您使用的是 InProc session state 并且不在 Web 场中运行。

Confusingly, the word “application” can have three different meanings:令人困惑的是,“应用程序”一词可以具有三种不同的含义:

  1. Your Web application (that is, the whole Web site)您的 Web 应用程序(即整个 Web 站点)
  2. One of the multiple HttpApplication instances that serve requests to your Web application向您的 Web 应用程序提供请求的多个HttpApplication实例之一
  3. The HttpApplication.Application property HttpApplication.Application属性

When the documentation says that the Application property returns “the current state of an application,” it means your Web application, not an individual HttpApplication instance.当文档说 Application 属性返回“应用程序的当前 state”时,这意味着您的 Web 应用程序,而不是单个 HttpApplication 实例。

It's true that if you add a member variable to your Global class (which inherits from HttpApplication), then the member variable won't be shared across HttpApplication instances.确实,如果将成员变量添加到全局 class(继承自 HttpApplication),则该成员变量将不会在 HttpApplication 实例之间共享。 But the Application property is special: it returns the same HttpApplicationState object no matter which HttpApplication instance you use to access the property.但是 Application 属性很特殊:无论您使用哪个 HttpApplication 实例访问该属性,它都会返回相同的 HttpApplicationState object。 Hence, any values you add via the Application property will be shared across all HttpApplication instances.因此,您通过 Application 属性添加的任何值都将在所有 HttpApplication 实例之间共享。 (And that's why you must call Lock and UnLock to synchronize access; you wouldn't need to do so if the HttpApplicationState object weren't shared.) (这就是为什么您必须调用 Lock 和 UnLock 来同步访问;如果 HttpApplicationState object 未共享,则不需要这样做。)

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

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