SignalR Hub 方法不再被调用

问题描述 投票:0回答:1

我在我的应用程序中使用 AspNetCore SignalR。我有服务器代码:

   public class Broadcast : Hub
   {
        public async Task AddRow(Guid tableId)
        {
            RecordView newRow = _tableService.AddRow(tableId);

            await Clients.Group(tableId.ToString()).SendAsync("AddRow", newRow);
        }

    }

我有客户端代码:

function addRow() {
    hubConnection.send("AddRow", useTables().tableId)
        .then(() => {
            console.log("AddRow Success!")
        })
        .catch((err) => {
            console.log("AddRow error: ", err)
        })
}

当我第一次调用 AddRow 方法时,一切正常。但下次该方法不会被调用。这是我在调试器中看到的。

我认为这是因为服务器一次只允许一个调用,并且服务器正在等待任务完成。如果我设置属性 MaximumParallelInitationsPerClient = 2,则 AddRow 方法会被调用两次,而第三次又不起作用。但我不明白为什么执行代码后任务不被认为完成:

await Clients.Group(tableId.ToString()).SendAsync("AddRow", newRow);
asp.net-core signalr signalr-hub asp.net-core-signalr
1个回答
0
投票

我解决了这个问题。在这行代码中

RecordView newRow = _tableService.AddRow(tableId);

我将一些数据写入数据库并使用 SaveChangesAsync 方法保存更改。当我使用 SaveChanges 方法修复它时,一切正常。

也许有人可以解释为什么会这样?

© www.soinside.com 2019 - 2024. All rights reserved.