Search code examples
signalr-hubsignalr.clientasp.net-core-signalr

SignaR js client negociate request 500


I am following the basic tutorial from msdn on how to use SignalR in .Net Core apps with a js client. You can find it here: https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.1&tabs=visual-studio

When I try to run the app to test this simple functionality I get a 500 negotiate POST error.

I even set the log level on the connection to see more details like this

const connection = new signalR.HubConnectionBuilder()
.withUrl("/chatHub")
.configureLogging(signalR.LogLevel.Trace)
.build();

All I can get from the logs is this: image with dev tool errors

This is my backend code: The ChatHub class

using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;

namespace Tasks.WebApp.Hubs
{
    public class ChatHub : Hub
    {
        public async Task SendMessage(string user, string message)
        {
            await Clients.All.SendAsync("Message", user, message);
        }
    }
}

And the config in the Startup class:

services.AddSignalR();

And:

app.UseSignalR(routes =>
        {
            routes.MapHub<ChatHub>("/chatHub");
        });

Do you have any insight on what am I doing wrong here? I am quite new to SignalR and JS, so maybe I did something wrong there, but I tried to follow the tutorial as strict as possible.

Thanks!


Solution

  • After digging through the Startup class, I found that the problem was related to the routes defined before. Found this question and the response here solved my issue.

    It is mandatory to put UseSignalR before UseMvc.