1

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=[%7B%22Name%22:%22/cucinahub%22%7D] - - 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=[%7B%22Name%22:%22/cucinahub%22%7D] - - - 404 0 - 122.4090ms

Where is the error? Thank you

andreat
  • 297
  • 2
  • 15

2 Answers2

0

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

Frank M
  • 1,379
  • 9
  • 18
  • Thank you, I also try with a net6 console app, but the result is the same, 404 not found. – andreat Jan 30 '23 at 20:42
  • I would post a new question with code for your client connection, method and hub class from just 1 of the versions. That way you can get specific help without the version difference being an issue. – Frank M Jan 30 '23 at 23:08
  • You may misunderstand the question, it can be done and I will post the answer latter. – Xiaotian Yang Jan 31 '23 at 09:44
  • I'm positive the documentation states you cannot mix the older .Net SignalR client/server with the .CORE client/version as linked in the SO answer. Maybe the 404 response is not exactly the same issue but even if that was worked through they would end up with the next error of a 405 'Method Not Allowed' due to the incompatibility of the 2 varying SignalR versions. – Frank M Jan 31 '23 at 13:02
0

The key is using the package Microsoft.AspNetCore.SignalR.Client on the WinForm project as client. It is available on .NETFramework. You can see from the picture here: enter image description here

For testing purpose, I created one webapi project and another winform project. Winform project has been installed the package. Then you can follow the MS document to set up the hubs in webapi project.

Then start the webapi project first to see which port it's running on. Now you can follow the MS Document for setting Clients to set client connect to hub. For testing purpose, I created two button functions to connect hub and send message to hub, here is the code of connect and send message function button:

private void btnconnect_Click(object sender, EventArgs e)
    {
        try
        {
            connection.StartAsync().Wait();
        }
        catch (Exception ex)
        {
            //...
        }
    }

private void btnsend_Click(object sender, EventArgs e)
    {
        try
        {
             connection.InvokeAsync("SendMessage",
                "winformclient", "hello world").Wait();
        }
        catch (Exception ex)
        {
            //...
        }
    }

And here is the code for setting up client connecting(Remember using your own port here):

HubConnection connection;
    public Form1()
    {
        InitializeComponent();

        connection = new HubConnectionBuilder()
            .WithUrl("https://localhost:7090/ChatHub")
            .Build();

        connection.Closed += async (error) =>
        {
            await Task.Delay(new Random().Next(0, 5) * 1000);
            await connection.StartAsync();
        };
    }

Now start the WinForm Application, click the send button and you can get the result here: enter image description here

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

Xiaotian Yang
  • 499
  • 1
  • 2
  • 7