【发布时间】:2019-05-20 13:52:02
【问题描述】:
我正在为服务器使用控制台应用程序,而我的客户端是 Angular 2 应用程序。我收到一个错误
错误:无法启动连接:错误:检测到与 ASP.NET SignalR 服务器的连接尝试。此客户端仅支持连接到 ASP.NET Core SignalR 服务器。
我设置了集线器,我的 Startup.cs 如下所示:
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Branch the pipeline here for requests that start with "/signalr"
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
// You can enable JSONP by uncommenting line below.
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
// EnableJSONP = true
};
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
hubConfiguration.EnableDetailedErrors = true;
map.RunSignalR(hubConfiguration);
});
}
}
在我的 Angular 应用中,这就是我在 app.component.ts 中所拥有的
ngOnInit(): void {
this.nick = window.prompt('Your name:', 'John');
this.hubConnection = new HubConnectionBuilder().withUrl("http://localhost:8089/signalr").build();
this.hubConnection
.start()
.then(() => console.log('Connection started!'))
.catch(err => console.log('Error while establishing connection :('));
this.hubConnection.on('addMessage', (nick: string, receivedMessage: string) => {
const text = `${nick}: ${receivedMessage}`;
this.messages.push(text);
});
}
public sendMessage(): void {
this.hubConnection
.invoke('sendToAll', this.nick, this.message)
.catch(err => console.error(err));
}
我知道我的错误是连接到 asp.net core signalr 服务器,但我该怎么做?
【问题讨论】:
-
ASP.NET SignalR (Microsoft.AspNet.SignalR.*) 和 ASP.NET Core SignalR (Microsoft.AspNetCore.SignalR.*) 互不兼容,但客户端应该可以在任何.NET Core 或 .NET Framework 应用程序(包括 ASP.NET Core),只要您正在与运行匹配版本的服务器通信。
标签: c# asp.net angular signalr