Failed to start the connection: Error: Unable to initialize any of the available transports

Failed to start the connection: Error: Unable to initialize any of the available transports

我遵循了代码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 核心版本 1.0.0-preview1-final 但是 @aspnet/signalr 我使用的是 1.0.0.

因此,我通过将 @aspnet/signalr 版本从 1.0.0 更改为 1.0.0-preview1-final 解决了该问题。 Github 已更新。