如何让 .Net Framework SignalR 客户端连接到 .Net Core SignalR Server?

How to get a .Net Framework SignalR client to connect to .Net Core SignalR Server?

这基本上是这个问题的副本,但存储库已存档,所以我不能在那里提问: https://github.com/aspnet/SignalR/issues/1645

我正在尝试让旧应用程序 运行 .NET Framework 连接到使用 SignalR 的较新应用程序 运行 .NET Core。 我读过一些帖子说 SignalR 的 AspNetCore 和 AspNet 版本不兼容,所以目标是在 .NET 上获得 Microsoft.AspNetCore.SignalR.Client 运行框架应用。

我已经使用框架版本 4.6.1 制作了一个测试项目,我相信它是 .NET Standard 2.0,所以根据 davidfowl 的评论,我应该能够安装这个包 https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client/1.0.0-preview1-final 我无法在我的 4.6.1 项目上安装这个包,所以我一定是做错了什么。

我还尝试了另一种解决方法,即使用 Microsoft.AspNetCore.SignalR.Client v3.1.6 代码创建一个 netstandard2.0 库,然后从我的 4.6 中引用 .dll。 1 个项目。

我的 netstandard2.0 库中的 3.1.6 代码如下所示:

using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Concurrent;
public class SignalRConnection
{       
    private readonly HubConnection hubConnection;

    public SignalRConnection(string endpoint)
    {
        this.endpoint = endpoint;
        hubConnection = new HubConnectionBuilder()
         .WithUrl(endpoint)
         .Build();
    }
}

我通过转到我的 4.6.1 项目中的引用引用了这个,右键单击并按添加引用并选择我的 netstandard2.0 项目。 我调用它的 4.6.1 代码是:

class Program
{
   static void Main(string[] args)
   {
      SignalRConnection connection = new SignalRConnection("http://localhost:8080/status");
   }
}

我想这可能是因为我没有使用 1.0.0,所以我也尝试了那个版本。我得到了相同的异常 FileNotFoundException 但对于每个不同的版本。

 System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.AspNetCore.SignalR.Client.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The system cannot find the file specified.'

有没有人有幸使用 SignalR 将 .NET Framework 应用程序连接到 .NET Core 应用程序,并且可以就我哪里出错提供一些指示?谢谢

编辑:

我能够使用 VS 2019 将它安装到 4.6.1 项目中,但是当我尝试将它安装到 VS 2015 中的 4.6.1 项目时,出现了上述错误

感谢 Camilo Terevinto 给我答案。 如果您使用的是 VS 2019,那么这已经解决了(只需使用 nuget 并安装)

但如果您需要使用 VS 2015,则由于 nuget 已过时,它无法正常工作。

https://www.nuget.org/downloads

安装最新的 nuget.exe

运行 .\nuget.exe 安装 Microsoft.AspNetCore.SignalR.Client -OutputDirectory C:\Projects\SignalR\ConsoleApplication1\ConsoleApplication1\packages

然后用添加的包手动更新你的 .csproj/packages.config(注意我还需要将 netstandard 添加到我的项目中才能工作) 对于我的 .csproj:

<ItemGroup>
    <Reference Include="netstandard" />
    <Reference Include="Microsoft.AspNetCore.SignalR.Client.Core, Version=3.1.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
        <HintPath>..\packages\Microsoft.AspNetCore.SignalR.Client.Core.3.1.8\lib\netstandard2.0\Microsoft.AspNetCore.SignalR.Client.Core.dll</HintPath>
    </Reference>
...

安装这个包时添加了很多参考,所以只需添加它们(我没有在这里添加它们,因为有 20 多个)

然后为您的packages.config做同样的事情:

<?xml version="1.0" encoding="utf-8"?>
<packages>
    <package id="Microsoft.AspNetCore.Connections.Abstractions" version="3.1.8" targetFramework="net461" />
...

完成后我刚刚重新启动了我的项目并且能够使用 signalR 客户端