Search code examples
c#asp.netasp.net-coresignalr

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


This is basically a copy of this question but the repository has been archived so I can't ask it there: https://github.com/aspnet/SignalR/issues/1645

I am trying to get an older application running .NET Framework to connect to a newer application running .NET Core using SignalR. I've read some posts that say that the AspNetCore & AspNet versions of SignalR are incompatible, so the goal is to get a Microsoft.AspNetCore.SignalR.Client running on the .NET Framework application.

I've made a test project using framework version 4.6.1, which I believe is .NET Standard 2.0 so I should be able to install this package https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Client/1.0.0-preview1-final according to a comment by davidfowl I'm unable to install this package on my 4.6.1 project so I must be doing something wrong.

I also tried another workaround which was creating a netstandard2.0 library using the Microsoft.AspNetCore.SignalR.Client v3.1.6 code, and then referencing the .dll from my 4.6.1 project.

The 3.1.6 code in my netstandard2.0 library looks like this:

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();
    }
}

I've referenced this by going to references in my 4.6.1 project, right clicking and pressing add reference and selecting my netstandard2.0 project. The 4.6.1 code that I am calling this with is:

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

I thought this might be because I'm not using 1.0.0, so I also tried with that version. I get the same exception FileNotFoundException but for each different version.

 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.'

Cannot load Microsoft.AspNetCore.SignalR.Client.Core

Has anyone had any luck connecting a .NET Framework app to a .NET Core app using SignalR and could give me some pointers on where I've went wrong? Thank you

Edit:

Nuget SignalR error

I was able to get this to install in a 4.6.1 project using VS 2019, but when I tried to install it into a 4.6.1 project in VS 2015 it gave me the above error


Solution

  • Thanks to Camilo Terevinto for giving me the answer for this. If you are using VS 2019 then this is solved already (just use nuget and install)

    But if you need to use VS 2015, then it is not working due to nuget being out of date.

    Install the most up to date nuget.exe from https://www.nuget.org/downloads

    Run .\nuget.exe install Microsoft.AspNetCore.SignalR.Client -OutputDirectory C:\Projects\SignalR\ConsoleApplication1\ConsoleApplication1\packages

    Then manually update your .csproj/packages.config with the added packages (note I also needed netstandard added to my project for it to work) For my .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>
    ...
    

    There are a lot more references added when installing this package so just add them all (I'm not adding them all here since there are 20+)

    Then do the same for your packages.config:

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

    Once done I just restarted my project and was able to use the signalR client