【问题标题】:Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server检测到与 ASP.NET SignalR 服务器的连接尝试。此客户端仅支持连接到 ASP.NET Core SignalR 服务器
【发布时间】:2019-05-20 13:52:02
【问题描述】:

我正在为服务器使用控制台应用程序,而我的客户端是 Angular 2 应用程序。我收到一个错误

错误:无法启动连接:错误:检测到与 ASP.NET SignalR 服务器的连接尝试。此客户端仅支持连接到 ASP.NET Core SignalR 服务器。

我设置了集线器,我的 Startup.cs 如下所示:

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Branch the pipeline here for requests that start with "/signalr"
            app.Map("/signalr", map =>
            {
                // Setup the CORS middleware to run before SignalR.
                // By default this will allow all origins. You can 
                // configure the set of origins and/or http verbs by
                // providing a cors options with a different policy.
                map.UseCors(CorsOptions.AllowAll);
                var hubConfiguration = new HubConfiguration
                {
                    // You can enable JSONP by uncommenting line below.
                    // JSONP requests are insecure but some older browsers (and some
                    // versions of IE) require JSONP to work cross domain
                    // EnableJSONP = true
                };
                // Run the SignalR pipeline. We're not using MapSignalR
                // since this branch already runs under the "/signalr"
                // path.

                hubConfiguration.EnableDetailedErrors = true;
                map.RunSignalR(hubConfiguration);
            });
        }
    }

在我的 Angular 应用中,这就是我在 app.component.ts 中所拥有的

ngOnInit(): void {
    this.nick = window.prompt('Your name:', 'John');

    this.hubConnection = new HubConnectionBuilder().withUrl("http://localhost:8089/signalr").build();

    this.hubConnection
      .start()
      .then(() => console.log('Connection started!'))
      .catch(err => console.log('Error while establishing connection :('));

    this.hubConnection.on('addMessage', (nick: string, receivedMessage: string) => {
      const text = `${nick}: ${receivedMessage}`;
      this.messages.push(text); 
    });
  }

  public sendMessage(): void {
    this.hubConnection
      .invoke('sendToAll', this.nick, this.message)
      .catch(err => console.error(err));
  }

我知道我的错误是连接到 asp.net core signalr 服务器,但我该怎么做?

【问题讨论】:

  • ASP.NET SignalR (Microsoft.AspNet.SignalR.*) 和 ASP.NET Core SignalR (Microsoft.AspNetCore.SignalR.*) 互不兼容,但客户端应该可以在任何.NET Core 或 .NET Framework 应用程序(包括 ASP.NET Core),只要您正在与运行匹配版本的服务器通信。

标签: c# asp.net angular signalr


【解决方案1】:

正如 Stefano 和 Ibanez 所说,“版本”存在问题。

您使用的 SignalR 客户端能够连接到 ASPNET Core,但不能连接到 ASPNET 服务器,如提到的错误。

如果您知道 ASPNET Core 是从基于多平台的 .Net Framework (CLR) 中分离出来的。

那么在这种情况下你有两个选择。

首先,如果您希望继续使用 ASPNET 服务器端,您可以更改您的客户端。 然后将您使用的库更改为支持 ASPNET 的库。 给个看看:SIGNALR - ASPNET vs ASPNET Core

其次,您可以更改服务器端并使用 ASPNET Core for SignalR,例如作为微服务。然后继续使用 ASPNET Core SignalR 库实现您的客户端。

【讨论】:

  • 我特别关心的是从后端记录日志 - 响应客户端建立的连接,如果出现任何问题,记录和错误。
  • 但是您应该知道您使用的是 .Net Core 还是 .Net Framework 基础架构。然后为此目的使用客户端库。或者,例如,如果您用作微服务,则将后端更改为相对于 signalR 端点的客户端库使用。 .Net Core 和 .Net Framework 是不同的东西。
【解决方案2】:

不确定,但您的问题与您正在使用的客户端库有关

发生错误是因为新的 SignalR 不允许您使用 old server & new clientnew server & old client look thisthis

您可能的状态:

根据您的示例,like this 他尝试使用 Angular 5 连接到旧的 singleR 服务器。我们可以从它如何尝试获取 HubConnection 实例来理解:look here

在您的情况下,您使用了 HubConnectionBuilder,因此您编写了新的核心 singleR。报错的原因可能是引用的库已经过时了。

import {Component, OnInit} from '@ angular / core';
import {HubConnection} from '@ aspnet / signalr-client';

可能的解决方案:

更新

@aspnet/signalr-client

@aspnet/signalr

【讨论】:

    【解决方案3】:

    您的问题解决了吗?正如 JohnB 所提到的,这可能是核心客户端尝试访问 .NET 框架集线器的问题。

    如果您尝试连接到 .NET 框架集线器,您将需要使用 signalr 包。

    否则,如果您的 hub 是一个 .NET 核心应用程序,您将改为使用 @aspnet/signalr

    【讨论】:

    • 我特别关心的是从后端记录日志 - 响应客户端建立的连接,如果出现任何问题,记录和错误。
    猜你喜欢
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-09
    相关资源
    最近更新 更多