【问题标题】:Why is SignalR getting a 404 on the negotiate call in my Angular web app?为什么 SignalR 在我的 Angular Web 应用程序中的协商呼叫中收到 404?
【发布时间】:2018-06-26 14:15:21
【问题描述】:

我正在尝试在我的 Angular 6 应用程序中初始化 SignalR,并为集线器调用单独的 ASP.Net webapi。

CORS 设置正确并允许呼叫通过,但是,我现在遇到此错误:

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

我已经安装了 1.0 的 @aspnet/signalr typescript NPM 库,我正在像这样初始化它:

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 :('));
    }
}

我的 Startup 类定义为:

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();
        }
    }
}

和我的中心类:

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);
        }
    }
}

我做错了什么?

【问题讨论】:

  • 根据我对 SignalR 的理解,这不是它的工作原理……您有一些资源可以用作指导吗?

标签: asp.net angular typescript signalr


【解决方案1】:

有一些问题:

1.在集线器中,您已经拥有 clients 属性

您从 Hub 类继承“Clients”属性。 以下就足够了:

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

2。连接的 URL 不匹配

同样在服务器部分你应该配置一些与 url 匹配的东西。服务器如何知道集线器正在“http://devapi.mywebapi.com/srtest”上运行?

3.看起来您正在混合 signalR 核心和“旧”信号器。

客户端和服务器必须具有相同的版本。

我建议你阅读https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client

如果您使用的是信号器核心,请阅读:https://docs.microsoft.com/en-us/aspnet/core/signalr/javascript-client?view=aspnetcore-2.1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-03
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多