c# - 无法让 SignalR Hub 变量停止丢失值(静态变量不是问题)

标签 c# signalr

我有一个简单的 SignalR 服务器,我几乎没有使用它的经验。

我的中心类是:

public class MyHub : Hub
{
    public List<Player> players { get; set; }
    public MyHub()
    {
        players = new List<Player>();
    }
    public void Searching(Player player)
    {
        players.Add(player);
        //Clients.All.a
    }
}

这是我在代码中调用搜索方法和变量的地方

    public String UserName { get; set; }
    public IHubProxy HubProxy { get; set; }
    const string ServerURI = "http://localhost:5051/signalr";
    public HubConnection Connection { get; set; }
    public Player User { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        User = new Player(100);
        ConnectAsync();
    }

    private void btnSearch_Click(object sender, RoutedEventArgs e)
    {
        HubProxy.Invoke("Searching", User);
    }

在 MyHub 类中,我希望玩家列表(目前)跟踪每次有人单击客户端上的 btnSearch 时,通过这样做,我只需将 Player 类的成员添加到玩家列表中我的中心。

由于某种原因,每次我调用它时,使用调试器我都可以看到列表再次为空,构造函数再次被调用,并且玩家列表再次为空。

我在其他堆栈溢出问题中看到问题是静态变量,但我的问题中没有任何内容是静态的。 (Player 类中的任何内容都不是静态的)

最佳答案

基本上,集线器是暂时的,这意味着您无法知道实例何时会被回收或丢弃。解决这个问题的一种方法是使用单例状态,但有很多选择。这直接来自 MSDN [1]:

"You don't instantiate the Hub class or call its methods from your own code on the server; all that is done for you by the SignalR Hubs pipeline. SignalR creates a new instance of your Hub class each time it needs to handle a Hub operation such as when a client connects, disconnects, or makes a method call to the server.

Because instances of the Hub class are transient, you can't use them to maintain state from one method call to the next. Each time the server receives a method call from a client, a new instance of your Hub class processes the message. To maintain state through multiple connections and method calls, use some other method such as a database, or a static variable on the Hub class, or a different class that does not derive from Hub. If you persist data in memory, using a method such as a static variable on the Hub class, the data will be lost when the app domain recycles.

If you want to send messages to clients from your own code that runs outside the Hub class, you can't do it by instantiating a Hub class instance, but you can do it by getting a reference to the SignalR context object for your Hub class. For more information, see How to call client methods and manage groups from outside the Hub class later in this topic."

[1]: https://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server "SignalR documentation"

关于c# - 无法让 SignalR Hub 变量停止丢失值(静态变量不是问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40343450/

相关文章:

c# - 为什么相同 IEnumerable 上的 Enumerable.Concat 被 SonarQube 识别为错误?

c# - cefsharp改变传给网站的参数屏幕的宽高c#

c# - SignalR WebSocket 握手 : Sec-WebSocket-Accept header is missing

java - 在C#中使用Androidjavaobject将二维数组传输到Java是可能的吗?并且也返回

c# - 使用 LINQ Lambda 表达式获取值为 NULL 的列

c# - 如何在 TcpClient 类中使用 SSL

sql-server - SqlDependency 与 SQLCLR 对 WebService 的调用

java - 如何在HubProxy signalr android上使用invoke

authentication - Blazor Server Signalr 中心缺少用户声明

asp.net - ASP.NET Core SignalR 是否向后兼容 ASP.NET SignalR