【问题标题】:Unhandled Rejection (Error): Invocation canceled due to the underlying connection being closed未处理的拒绝(错误):由于基础连接被关闭而取消调用
【发布时间】:2020-08-15 23:58:53
【问题描述】:

我正在尝试从 Mongo 数据库收集数据并将数据返回给 SignalR Javascript 客户端。我正在使用 .net 核心。

返回的数据是一个List。服务器上的代码遇到断点,当我单步执行代码时,它会按预期返回数据列表。但是,SignalR 客户端会引发异常...

Unhandled Rejection (Error): Invocation canceled due to the underlying connection being closed

为了好玩,我更改了 Mongo 查询以从列表中返回一个 count(),这就像一个魅力。我还更改了集线器以返回一个空列表(不查询 Mongo),这也有效……我觉得 Mongo 是问题所在。这是我第一个使用 MongoDB 的项目。

这是我的客户端代码(Javascript)

this.hub = new signalR.HubConnectionBuilder()
  .withUrl("/api")
  .build();    
this.hub.start({transport: 'webSockets'}).then(() => {
  console.log("STATE", this.hub.state); // state is "Connected" at this point.
  this.hub.invoke("GetData").then((data) => {
    console.log(data); // We never reach this line
  });
});

在服务器上,我的集线器中有这段代码:

    public List<Data> GetData() {
        return this.GetService().GetData();
        // return new List<Data>(); // If I replace the lines above with this, it works??? Has to be MongoDB?
    }

我的服务器端服务代码是:

public List<Data> GetData() {
     MongoClient client = new MongoClient(config.GetConnectionString("MyDatabase"));
     IMongoDatabase database = client.GetDatabase("MyDatabase");
     IMongoCollection<Data> data = database.GetCollection<Data>("Data");
     return data
        .Find(t => !t.Deleted)
        .ToList();

}

我想知道 Mongo 是否试图在数据流回客户端时以某种方式更改数据。

在通过 SignalR 将 IMongoCollection 发送到客户端之前,我将其转换为列表。

【问题讨论】:

  • 你在关注什么文档?

标签: javascript c# mongodb .net-core signalr


【解决方案1】:

在进行以下更改后,我可以确认它现在可以工作了:

Startup.cs

services.AddSignalR().AddAzureSignalR(options =>
{
    options.ServerStickyMode = Microsoft.Azure.SignalR.ServerStickyMode.Required;
});

【讨论】:

    猜你喜欢
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 2019-08-31
    相关资源
    最近更新 更多