1

I have an ASP.NET Core web API with a SignalR Hub and a .NET Core console app that acts as a client for the hub.

The console app throws the following error when trying to connect:

System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred. (StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
  Date: Mon, 20 May 2019 18:12:44 GMT
  Server: Kestrel
  Content-Length: 0
})
  Source=System.Private.CoreLib
  StackTrace:
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at SurveyApp.NotificationClient.Program.Main(String[] args) in C:\Users\user\source\repos\SurveyApp\SurveyApp.NotificationClient\Program.cs:line 16

Inner Exception 1:
HttpClientException: StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
  Date: Mon, 20 May 2019 18:12:44 GMT
  Server: Kestrel
  Content-Length: 0
}

I have tried all kinds of renaming of routes and hubs, but it still doesn't work. Also tried the solution shown here since it is a similar issue, but still not result: HttpClientException when connecting to working hub from SignalR .NET Client

Server: DI registration:

            services.AddSignalR();

Middleware registration:

            ...

            app.UseSignalR(route =>
            {
                route.MapHub<NotificationHub>("/notificationhub");
            });
            app.UseMvc();

Hub class:

public class NotificationHub : Hub
    {
        public Task SendMessage(string message)
        {
            return Clients.All.SendAsync(message);
        }

        public override Task OnConnectedAsync()
        {
            Console.WriteLine("A client connected");
            return base.OnConnectedAsync();
        }
    }

Client code:

static void Main(string[] args)
        {
            var connection = new HubConnection("https://localhost:5001/notificationhub");
            connection
                .CreateHubProxy("NotificationHub")
                .On<string>("ReceiveNotification", Console.WriteLine);
            connection.Start().Wait();
            Console.WriteLine("Connection started");
            Console.ReadKey();
        }

When I don't call Wait() after connection.Start(), I do not get any error, but the client still doesn't connect. I'm running .NET Core 2.2 both on the server and the client and the version for the SignalR nuget is 2.4.1

Stevie
  • 149
  • 6
  • 17

1 Answers1

4

var connection = new HubConnection("https://localhost:5001/notificationhub");

This construction doesn't exist in ASP.NET Core any more. Seems that you're connecting an ASP.NET Core SignalR server with a Microsoft.AspNet.SignalR.Client, which should work with ASP.NET SignalR and is not compatible with ASP.NET Core.

If that's the case, you need add a reference to Microsoft.AspNetCore.SignalR.Client which targets .NETStandard 2.0 and should be able to run on .NET Framework too.

And then create a connection as below :

var Connection = new HubConnectionBuilder()
    .WithUrl("https://localhost:5001/notificationhub")
    .ConfigureLogging(logging =>{
        logging.AddConsole();
    })
    .Build();

For more details, see https://learn.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-2.2#connect-to-a-hub

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
itminus
  • 23,772
  • 2
  • 53
  • 88