SignalR - Client connection closes straight after starting - InvalidOperationException: Sequence contains no elements
Asked Answered
G

1

9

I have a a SignalR client which seems to close straight after starting, the error message i get is:

"The server closed the connection with the following error: Connection closed with an error. InvalidOperationException: Sequence contains no elements"

The SignalR client is being used in a ASP.Net Core Web API project (within an API controller).

The Nuget package i am using is called 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
    }
Guggenheim answered 10/1, 2019 at 22:58 Comment(1)
In your signalr hub, do you use OnConnectedAsync? Can you show the code?Zoltai
A
34

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

Astra answered 10/1, 2020 at 4:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.