asp.net-mvc - 启动连接失败: Error: Unable to initialize any of the available transports

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

我遵循了代码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/signalr1.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/

相关文章:

asp.net-mvc - 尝试使用 fiddler 获取 oauth token

c# - 如何替换某些段的值以保留其他段的值?

asp.net - 我的数据库没有与我的 MVC 应用程序一起部署

asp.net-mvc - 如何使用 ASP.NET MVC 2 的数据注释验证最好地处理后期数据验证?

javascript - 是否可以在 Angular Element 中包含一个 img src?

c# - Excel CSV 编码问题

c# - Entity Framework : Can DB Contexts Have Foreign Keys Across Different Schemas?

c# - API 中 POST 模型绑定(bind)问题

android - 添加后端到 Ionic 应用程序以从 MySQL 数据库读取

javascript - 如何从 Angular 6 或 7 组件内的 LESS 文件访问 style.less 中的 LESS 变量?