Home > Blockchain >  Add SignalR to Net6 Web Api and connect from Winforms application (.net fx4.8) - hub not found
Add SignalR to Net6 Web Api and connect from Winforms application (.net fx4.8) - hub not found

Time:02-01

I'm trying to add signalr to the webapi, I create the CucinaHub class:

public class CucinaHub : Hub
{
    static ConcurrentDictionary<string, string> _users = new ConcurrentDictionary<string, string>();

    #region Client Methods

    public void SetUserName(string userName)
    {
        _users[Context.ConnectionId] = userName;
    }

    #endregion Client Methods
}

and configure SignalR:

services.AddSignalR();

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapHub<CucinaHub>("/cucinahub");
        });

Then in Windows form application I use this code to connect with the hub:

        _signalRConnection = new HubConnection($"{Properties.Settings.Default.URL}/api", true);

        _hubProxy = _signalRConnection.CreateHubProxy("/cucinahub");

        _hubProxy.On("GetValvole", async () => await GetValvole());
        
        try
        {
            //Connect to the server
            await _signalRConnection.Start();
        }
        catch (Exception ex)
        {
            Log.Error(ex.ToString());
        }

I get always 404 response code:

Hosting environment: Development Content root path: D:\SwDev\PigsutffHTML\Server\Common\Common.WebApiCore Now listening on: http://localhost:5000 Application started. Press Ctrl C to shut down. info: Microsoft.AspNetCore.Hosting.Diagnostics[1] Request starting HTTP/1.1 GET http://localhost:5000/api/signalr/negotiate?clientProtocol=2.1&connectionData=[{"Name":"/cucinahub"}] - - warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3] Failed to determine the https port for redirect. info: Microsoft.AspNetCore.Hosting.Diagnostics[2] Request finished HTTP/1.1 GET http://localhost:5000/api/signalr/negotiate?clientProtocol=2.1&connectionData=[{"Name":"/cucinahub"}] - - - 404 0 - 122.4090ms

Where is the error? Thank you

CodePudding user response:

Short answer - You cannot mix the .NET 4.x Framework and the .NET Core server/clients. Neither one can talk to the other. You have to upgrade your client. More info in this SO enter image description here

For testing purpose, I created one webapi project and another winform project. Both of them have been installed the package. Then you can follow the enter image description here

You can see from the screenshot, the hub has already got the message from WinForm application.

  • Related