本文介绍了SignalR Core-StatusCode:404,ReasonPhrase:“未找到",版本:1.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个项目.

首先,WebApi包含使用SignalR的集线器:

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);
    }
}

我在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"));
    }

我相信我会在TaskController.cs中发出这样的通知:

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);
    }

问题从这里开始.

我有包含HubServiceWPF应用:

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();
    }
}

然后在我的MainViewModel构造函数中将其初始化:

And i initialize it in my MainViewModel constructor:

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

问题开始于await hubConnection.Start();.从这一行,我得到一个错误:

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

我的问题是,我做错了什么以及如何在我的WebApi项目中连接到Hub?

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

编辑

集线器似乎正常工作.我在浏览器中输入https://localhost:44365/notifications并收到消息:

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

EDIT2

WPF项目是.NET Framework 4.7.2WebApiCore 2.1.

推荐答案

我找到了解决方案.

在我发现的互联网上寻找它,对Microsoft.AspNet.SignalR的引用将不适用于基于Core的SignalR服务器.

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

我需要更改.NET Framework 4.7.2项目中的某些内容.首先,删除对AspNet.SignalR的引用,并将其更改为Core.为此,只需在您的.Net Framework proj中安装此nuget:

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:

然后,该服务编译没有错误(idk,如果它仍然可以工作,但是我在WebApi上使用Core SignalR将Connected的连接计数设为1):

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();
    }
}

现在,我的WPF毫无例外地进行编译.我正在从服务器获取通知

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

这篇关于SignalR Core-StatusCode:404,ReasonPhrase:“未找到",版本:1.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:08