我遵循了代码SignalR guide for .NET CORE AngularApp
我收到以下错误:
Failed to start the connection: Error: Unable to initialize any of the available transports
代码托管在 Microsoft 的 Github here 上
下面是来自 Startup.cs
代码的代码片段:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("CorsPolicy", builder => {
builder
.AllowAnyMethod().AllowAnyHeader()
.WithOrigins("http://localhost:49446")
.AllowCredentials();
//.AllowAnyOrigin()
}));
services.AddSignalR();
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("CorsPolicy");
app.UseSignalR(routes =>
{
routes.MapHub<NotifyHub>("/notifyhub");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
...
}
}
我的中心类(class):
public class NotifyHub:Hub<ITypedHubClient>
{
public NotifyHub()
{
}
}
Angular app.component.ts
:
import {Component} from '@angular/core';
import {HubConnection, HubConnectionBuilder, IHubProtocol} from '@aspnet/signalr';
@Component({selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css']})
export class AppComponent {
public _hubConnection : HubConnection;
msgs : Message[] = [];
constructor() {}
ngOnInit() : void {
let builder = new HubConnectionBuilder();
this._hubConnection = builder
.withUrl('/notifyhub')
.build();
this
._hubConnection
.start()
.then(() => console.log('Connection started!'))
.catch(err => console.log('Error :', err));;
this
._hubConnection
.on('BroadcastMessage', (type : string, payload : string) => {
this
.msgs
.push({severity: type, summary: payload});
});
}
}
不确定我在这里错过了什么?请指教。谢谢。
最佳答案
好的,所以问题是 .NET Core
版本和 @aspnet/signalr
版本不匹配。
我正在使用.NET core版本1.0.0-preview1-final
但我使用的是@aspnet/signalr
是1.0.0
。
因此,我通过将 @aspnet/signalr
版本从 1.0.0
更改为 1.0.0-preview1-final
解决了该问题这解决了这个问题。 Github已更新。
关于asp.net-mvc - 启动连接失败: Error: Unable to initialize any of the available transports,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50683088/