asp.net - 为什么 SignalR 在我的 Angular Web 应用程序中的协商调用中收到 404?

标签 asp.net angular typescript signalr

我正在尝试在我的 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)

我已经安装了 @aspnet/signalr typescript NPM 库的 1.0,我正在像这样初始化它:
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);
        }
    }
}

我究竟做错了什么?

最佳答案

有一些问题:

1. 在集线器内,您已经拥有客户端属性

您从集线器类继承“客户端”属性。
以下就足够了:

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

2.连接url不匹配

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

3. 看起来您正在混合使用 signalR 核心和“旧”signalr。

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

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

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

关于asp.net - 为什么 SignalR 在我的 Angular Web 应用程序中的协商调用中收到 404?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51045096/

相关文章:

asp.net - 我是否真的无法在商业应用程序中使用 Codeplex 的 Telerik Asp.NET MVC 控件?

asp.net - VS2015有调试webapi路由的工具吗?

javascript - jquery 中未选择子元素

javascript - 在 Angular 4 中使用等待时生成器未定义错误

Angular 8 : TypeError: Found non-callable @@iterator

javascript - 如何键入类或构造函数

javascript - Angular Universal 和浏览器功能检查

mysql - 删除和更新命令不起作用......asp.net2010、SQLYOG DATABASE 5.0 中的程序

angular - 如何在 Angular 5 中向 ng-select 添加 "select all"功能

javascript - 如何使用守卫nestjs进行e2e