简体   繁体   English

SignalR - 客户端连接在启动后立即关闭 - InvalidOperationException:序列不包含任何元素

[英]SignalR - Client connection closes straight after starting - InvalidOperationException: Sequence contains no elements

I have aa SignalR client which seems to close straight after starting, the error message i get is:我有一个 SignalR 客户端,它似乎在启动后立即关闭,我得到的错误消息是:

"The server closed the connection with the following error: Connection closed with an error. InvalidOperationException: Sequence contains no elements" “服务器关闭连接,出现以下错误:连接关闭,出现错误。InvalidOperationException:序列不包含元素”

The SignalR client is being used in a ASP.Net Core Web API project (within an API controller). SignalR 客户端用于 ASP.Net Core Web API 项目(在 API 控制器内)。

The Nuget package i am using is called Microsoft.AspNetCore.SignalR.Client (v 1.1.0)我使用的 Nuget 包称为 Microsoft.AspNetCore.SignalR.Client (v 1.1.0)

My code looks like this:我的代码如下所示:

    try
    {
        //SEND MESSAGE TO HUB
        var connection = new HubConnectionBuilder()
            .WithUrl("https://sample.azurewebsites.net/ChatHub")
            .Build();

        connection.Closed += async (error) =>
        {
            //log error - this is getting called straight after StartAsync
        };

        await connection.StartAsync();

        await connection.InvokeAsync("SendToATeam", "x", "y");

        await connection.StopAsync();            
    }
    catch (Exception ex)
    {
        //log error
    }

On your server you can turn on detailed errors via:在您的服务器上,您可以通过以下方式打开详细错误:

services.AddSignalR(o =>
{
  o.EnableDetailedErrors = true;
})

This will then give you a more detailed error message on the client这将在客户端上为您提供更详细的错误消息

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

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