我遵循了代码SignalR guide for .NET CORE AngularApp
我得到以下错误:
无法启动连接:错误:无法初始化任何可用的传输
代码存在于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?}");
});
...
}
}
My Hub课程:
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});
});
}
}
不确定我在这里缺少什么?请指教。谢谢。
答案 0 :(得分:1)
好的,问题是.NET Core
版本和@aspnet/signalr
版本不匹配。
我使用的是.NET核心版1.0.0-preview1-final
但@aspnet/signalr
我使用的是1.0.0
。
因此,我通过将@aspnet/signalr
版本从1.0.0
更改为修复此问题的1.0.0-preview1-final
来解决了该问题。 Github已更新。