Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR Core - StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1

I have two projects.

First, WebApi that contains Hub for using SignalR:

public class NotificationsHub : Hub
{
    public async Task GetUpdateForServer(string call)
    {
        await this.Clients.Caller.SendAsync("ReciveServerUpdate", call);
    }
}

I set up that Hub in Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        // ofc there is other stuff here

        services.AddHttpContextAccessor();
        services.AddSignalR();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseSignalR(routes => routes.MapHub<NotificationsHub>("/Notifications"));
    }

I belive that i would make notifications like this in TaskController.cs:

    [HttpPost]
    public async Task<IActionResult> PostTask([FromBody] TaskManager.Models.Task task)
    {
        if (!this.ModelState.IsValid)
        {
            return this.BadRequest(this.ModelState);
        }

        this.taskService.Add(task);

        //here, after POST i want to notify whole clients
        await this.notificationsHub.Clients.All.SendAsync("NewTask", "New Task in database!");

        return this.Ok(task);
    }

The problem starts here.

I have WPF app that contains HubService:

public class HubService
{
    public static HubService Instance { get; } = new HubService();

    public ObservableCollection<string> Notifications { get; set; }

    public async void Initialized()
    {
        this.Notifications = new ObservableCollection<string>();

        var queryStrings = new Dictionary<string, string>
        {
            { "group", "allUpdates" }
        };

        var hubConnection = new HubConnection("https://localhost:44365/Notifications", queryStrings);
        var hubProxy = hubConnection.CreateHubProxy("NotificationsHub");

        hubProxy.On<string>("ReciveServerUpdate", call =>
        {
            //todo
        });

        await hubConnection.Start();
    }
}

And i initialize it in my MainViewModel constructor:

    public MainWindowViewModel()
    {
        HubService.Instance.Initialized();
    }

The problem starts in await hubConnection.Start();. From this line, i get an error:

"StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcQWRtaW5cc291cmNlXHJlcG9zXFRhc2tNYW5hZ2VyXFRhc2tNYW5hZ2VyLXdlYmFwaVxOb3RpZmljYXRpb25zXHNpZ25hbHJcbmVnb3RpYXRl?= Date: Tue, 28 May 2019 16:25:13 GMT Server: Kestrel X-Powered-By: ASP.NET Content-Length: 0

My question is, what im doing wrong and how to connect to Hub in my WebApi project?

EDIT

Hub seems to work. I typed into my browser: https://localhost:44365/notifications and i got message:

Connection ID required

EDIT2

WPF project is .NET Framework 4.7.2 and WebApi is Core 2.1.

like image 551
michasaucer Avatar asked May 28 '19 16:05

michasaucer


1 Answers

i found a solution.

Looking for it on the internet i discovered, that reference to Microsoft.AspNet.SignalR wont work with SignalR server based on Core.

I needed to change some things in my WPF project that is .NET Framework 4.7.2. First, delete reference to AspNet.SignalR and change it to Core one. To get this, just install this nugets in your .Net Framework proj:

enter image description here

Then, this service compiles without errors (idk if it works yet, but i get Connected count to 1 on my WebApi with Core SignalR):

using System.Collections.ObjectModel;
using Microsoft.AspNetCore.SignalR.Client;

public class HubService
{
    public static HubService Instance { get; } = new HubService();

    public ObservableCollection<string> Notifications { get; set; }

    public async void Initialized()
    {
        this.Notifications = new ObservableCollection<string>();

        var hubConnection = new HubConnectionBuilder()
            .WithUrl(UrlBuilder.BuildEndpoint("Notifications"))
            .Build();

        hubConnection.On<string>("ReciveServerUpdate", update =>
        {
            //todo, adding updates tolist for example
        });

        await hubConnection.StartAsync();
    }
}

Now my WPF compiles without exception. Im getting notifications from server

like image 194
michasaucer Avatar answered Sep 20 '22 09:09

michasaucer