Scottie
Scottie

Reputation: 11308

Why is SignalR getting a 404 on the negotiate call in my Angular web app?

I am attempting to initialize SignalR in my Angular 6 app and calling into a separate ASP.Net webapi for the hubs.

CORS is set up correctly and allowing the calls through, however, I am now stuck on this error:

Debug: Sending negotiation request: http://devapi.mywebapi.com/srtest/negotiate
http://devapi.mywebapi.com/srtest/negotiate 404 (Not Found)

I have installed 1.0 of the @aspnet/signalr typescript NPM library and I am initializing it like so:

import { Component, OnInit } from "@angular/core";
import { HubConnection, HubConnectionBuilder, LogLevel } from '@aspnet/signalr';

@Component({
    selector: "test",
    templateUrl: "test.component.html"
})
export class TestComponent implements OnInit {
    private hubConnection: HubConnection;

    constructor() { }

    ngOnInit() {
        this.hubConnection = new HubConnectionBuilder()
            .withUrl("http://devapi.mywebapi.com/srtest")
            .configureLogging(LogLevel.Debug)
            .build();

        this.hubConnection
            .start()
            .then(() => console.log('Connection started!'))
            .catch(err => console.log('Error while establishing connection :('));
    }
}

I have my Startup class defined as:

using Owin;
using Microsoft.Owin;

[assembly: OwinStartup(typeof(Api.Controllers.SignalR.Startup))]
namespace Api.Controllers.SignalR
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }
    }
}

and my hub class:

using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;
using System.Web.Http;

namespace Api.Controllers.SignalR
{
    public class SRTestHub : Hub
    {
        public void SendTest(string message)
        {
            var context = GlobalHost.ConnectionManager.GetHubContext<SRTestHub>();
            context.Clients.All.receiveMessage(message);
        }
    }
}

What am I doing wrong?

Upvotes: 4

Views: 7352

Answers (1)

Stephu
Stephu

Reputation: 3334

There are some problems:

1. Inside a hub you allready have the clients property

You inherit the "Clients" Property from Hub class. The following is enough:

public class SRTestHub : Hub
    {
        public void SendTest(string message)
        {
            Clients.All.receiveMessage(message);
        }
    }

2. Url of connection does not match

Also in the server part you should configure something that the url matches. How the server should know that the hub is running on "http://devapi.mywebapi.com/srtest"?

3. It looks like you are mixing signalR core and "old" signalr.

Client and server must have same versions.

I suggest you to read https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client

In the case you are using signalr core read: https://learn.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-2.1

Upvotes: 2

Related Questions