ASP.NET CORE 2.1 服务器在调试时超时

ASP.NET CORE 2.1 Server timeout while debugging

'Error: Server timeout elapsed without receiving a message from the server.'.

我正在尝试调试一些服务器端代码,当我这样做时,客户端在不到一分钟内断开连接。

我只使用 SignalR 与客户端通信,还没有控制器。

是否有任何设置可以禁用超时或至少使其比现在更长?

我的launchSettings.json:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:26793",
      "sslPort": 44386
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HTTPS_PORT": "44386"
      }
    },
    "Api": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_URLS": "https://localhost:5001;http://localhost:5000"
      }
    }
  }
}

感谢@Arhire Ionut

这是增加 Javascript 客户端超时的方法

hubConnection.serverTimeoutInMilliseconds = 100000; // 100 second

此处有更多详细信息 => https://github.com/aspnet/Docs/issues/6885

@MahmoudFarhat 在另一个答案中提到的是正确的。 But also take a look at this link 然后阅读下面我的评论。

如果 signalR 断开连接,您应该尝试重新建立连接。连接可能因其他几个原因而断开,包括用户切换网络。例如,如果用户正在使用蜂窝 phone 并连接到 home/office Wifi,但退出后连接到蜂窝数据连接。

要重新连接,您可以使用以下方法(对我来说很有用):

// re-establish the connection if connection dropped
connection.onclose(() => setTimeout(startSignalRConnection(connection), 5000));

其中 startSignalRConnection 是:

const startSignalRConnection = connection => connection.start()
  .then(() => console.info('Websocket Connection Established'))
  .catch(err => console.error('SignalR Connection Error: ', err));

连接是

const connection = new HubConnectionBuilder()
  .withUrl(connectionHub, options)
  .withHubProtocol(protocol)
  .build();

每隔一段时间发送一条消息 时间间隔小于 serverTimeoutInMilliseconds 所以在超时之前它会发送一条消息并且连接处于活动状态