Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Console SignalR Client vs .NET Core SignalR Server

I need to exchange data between a .NET Console and an ASP.NET Core 2.0 applications. The second already hosts a SignalR server:

public class MyHub : Hub
{
    public override async Task OnConnectedAsync()
    {
        await Clients.All.InvokeAsync("Send", $"{Context.ConnectionId} connected");
    }

    public Task Send(string message)
    {
        return Clients.All.InvokeAsync("Send", $"{Context.ConnectionId}: {message}");
    }
}

app.UseSignalR(routes =>
{
     routes.MapHub<MyHub>("hubs");
});

from the html pages I can invoke the functions. Hence it's working. Now I added to the solution a .NET 4.6 Console application:

public static HubConnection _connection;

static void Main(string[] args)
{
    _connection = new HubConnection("http://localhost:51278/hubs");
    var myHub = _connection.CreateHubProxy("MyHub");

    _connection.Closed += OnDisconnected;
    OnDisconnected();

    myHub.Invoke<string>("Send", "Hello World ").ContinueWith(task => {
        if (task.IsFaulted)
        {
            Console.WriteLine("There was an error calling send: {0}", task.Exception.GetBaseException());
        }
        else
        {
            Console.WriteLine(task.Result);
        }
    });

    _connection.Stop();
}

static void OnDisconnected()
{
    Console.WriteLine("Disconnected. Try to connect...");
    var t = _connection.Start(new LongPollingTransport());

    bool result = false;
    t.ContinueWith(task => 
    {
        if (!task.IsFaulted)
        {
            result = true;
            Console.WriteLine("Connected");
        }
    }).Wait();

    if (!result)
    {
        OnDisconnected();
    }
}

But the output is:

Disconnected. Try to connect...

Disconnected. Try to connect...

and nothing else. I'm new to SignalR and I don't know how might debug further such a behavior.

like image 616
Mark Avatar asked Jan 30 '23 02:01

Mark


1 Answers

SignalR for.Net Core is not compatible with the previous version of SignalR for the Full .Net framework.

SignalR clients written in the full framework cannot be used with hubs in Asp.Net core (and vice-versa).

Here is confirmation from GitHub

Will a SignalR Core server connect OK with 'traditional' Signalr 2 clients?

like image 89
pieperu Avatar answered Feb 04 '23 22:02

pieperu