locked
Aspnet core signalr console app connection example RRS feed

  • Question

  • User1448655074 posted

    Hi

    I have a server running .net 4.5.1 with SignalR (IIS)

    I want to connect to the server using a console client app on .net core 2,2.  I have installed the below Nuget package on the client

    Microsoft.AspNetCore.SignalR.Client;

    and have followed https://docs.microsoft.com/en-us/aspnet/core/signalr/dotnet-client?view=aspnetcore-2.2

    I keep getting the below error

    "There was an error opening the connection:System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found)."

    The name of my Hub on the server is correct so I'm not sure of the problem 

    I have also tried

    .WithUrl("http://172.10.0.20:81/client/?hub=myHub")

    please help

    connection = new HubConnectionBuilder()

    //.WithUrl("http://172.10.0.20:81/client/?hub=myHub")

    .WithUrl("http://172.10.0.20:81/myHub")

    .Build();

    connection.StartAsync().ContinueWith(task => {

    if (task.IsFaulted)

    {Console.WriteLine("There was an error opening the connection:{0}",

    task.Exception.GetBaseException());}

    else

    {Console.WriteLine("Connected");}}).Wait();

    connection.StartAsync();

    Sunday, May 26, 2019 10:37 AM

Answers

  • User61956409 posted

    Hi Poly,

    Response status code does not indicate success: 404 (Not Found).

    .WithUrl("http://172.10.0.20:81/myHub")

    Please check whether you setup SignalR routes correctly, like below. And you can refer to [this article](https://docs.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-2.2) to setup your SignalR hub.

    app.UseSignalR(route =>
    {
        route.MapHub<ChatHub>("/myHub");
    });

    Besides, I tried to host the SignalR Hub on my IIS server, and connect it from JavaScript Client and .NET Client (a console application), which work well on my side.

    Setup SignalR routes

    app.UseSignalR(routes =>
    {
        routes.MapHub<ChatHub>("/chatHub");
    });

    Hub Class

    public class ChatHub : Hub
    {
        public async Task SendMessage(string user, string message)
        {       
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }
    }

    In Console App, connect to hub and call hub method

    connection = new HubConnectionBuilder()
    .WithUrl("http://xxx.xxx.xxx.xxx:9520/chatHub")
    .Build();
    
    await connection.StartAsync();
    
    var mes = Console.ReadLine();
    
    await connection.InvokeAsync("SendMessage", "Consloe Client", mes);

    ReceiveMessage method definition

    connection.On<string, string>("ReceiveMessage",
    (string user, string message) =>
    {
        Console.WriteLine($"Message from {user}: {message}");
    });

    Test Result:

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 27, 2019 6:18 AM

All replies

  • User61956409 posted

    Hi Poly,

    Response status code does not indicate success: 404 (Not Found).

    .WithUrl("http://172.10.0.20:81/myHub")

    Please check whether you setup SignalR routes correctly, like below. And you can refer to [this article](https://docs.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-2.2) to setup your SignalR hub.

    app.UseSignalR(route =>
    {
        route.MapHub<ChatHub>("/myHub");
    });

    Besides, I tried to host the SignalR Hub on my IIS server, and connect it from JavaScript Client and .NET Client (a console application), which work well on my side.

    Setup SignalR routes

    app.UseSignalR(routes =>
    {
        routes.MapHub<ChatHub>("/chatHub");
    });

    Hub Class

    public class ChatHub : Hub
    {
        public async Task SendMessage(string user, string message)
        {       
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }
    }

    In Console App, connect to hub and call hub method

    connection = new HubConnectionBuilder()
    .WithUrl("http://xxx.xxx.xxx.xxx:9520/chatHub")
    .Build();
    
    await connection.StartAsync();
    
    var mes = Console.ReadLine();
    
    await connection.InvokeAsync("SendMessage", "Consloe Client", mes);

    ReceiveMessage method definition

    connection.On<string, string>("ReceiveMessage",
    (string user, string message) =>
    {
        Console.WriteLine($"Message from {user}: {message}");
    });

    Test Result:

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 27, 2019 6:18 AM
  • User61956409 posted

    Hi Poly

    I have a server running .net 4.5.1 with SignalR (IIS)

    In my previous reply, I am using AspNetCore SignalR build my hub.

    If your hub server is built with ASP.NET SignalR, but the client application is built with ASP.NET Core SignalR, the client application would not able to connect to the server, because they are not compatible.

    As I share in my previous reply, please refer to [this article](https://docs.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-2.2) to build your SignalR hub.

    With Regards,

    Fei Han

    Monday, May 27, 2019 8:40 AM
  • User1448655074 posted

    thanks that pushed me in the right direction.  

    I'm using signalR with webforms so the hub is located at 

    `http://172.10.0.20:82/signalr/myHub`

    After changing path I got the below informative error

    There was an error opening the connection:System.IO.InvalidDataException: Invalid negotiation response received. ---> System.InvalidOperationException: Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server.

    Monday, May 27, 2019 2:26 PM
  • User61956409 posted

    Hi Poly,

    Detected a connection attempt to an ASP.NET SignalR Server. This client only supports connecting to an ASP.NET Core SignalR Server.

    Your Hub server is built with AspNet.SignalR, if you'd like to build a console application to connect to your Hub, please refer to the following documentation to setup your client.

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-net-client#clientsetup

    With Regards,

    Fei Han

    Tuesday, May 28, 2019 1:27 AM