无法启动连接:错误:无法初始化任何可用的传输

时间:2018-06-04 14:38:32

标签: asp.net-mvc angular asp.net-core signalr asp.net-core-signalr

我遵循了代码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});
  });
 }
}

不确定我在这里缺少什么?请指教。谢谢。

1 个答案:

答案 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已更新。