【发布时间】: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