SignalR 核心 - 状态代码:404,原因短语:“未找到”,版本:1.1

mic*_*cer 5 c# signalr asp.net-core asp.net-core-2.1

我有两个项目。

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

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

我在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"));
    }
Run Code Online (Sandbox Code Playgroud)

我相信我会在以下时间发出这样的通知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);
    }
Run Code Online (Sandbox Code Playgroud)

问题从这里开始。

我有WPF包含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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的MainViewModel构造函数中初始化它:

    public MainWindowViewModel()
    {
        HubService.Instance.Initialized();
    }
Run Code Online (Sandbox Code Playgroud)

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

“的StatusCode:404,ReasonPhrase: '未找到',版本:1.1,内容:System.Net.Http.StreamContent,集管:X-SourceFiles:= UTF-8乙QzpcVXNlcnNcQWRtaW5cc291cmNlXHJlcG9zXFRhc2tNYW5hZ2VyXFRhc2tNYW5hZ2VyLXdlYmFwaVxOb3RpZmljYXRpb25zXHNpZ25hbHJcbmVnb3RpYXRl =日期:????星期二,2019年5月28日16:25:13 GMT 服务器:Kestrel X-Powered-By:ASP.NET 内容长度:0

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

编辑

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

需要连接 ID

编辑2

WPF项目是.NET Framework 4.7.2WebApiCore 2.1

mic*_*cer 7

我找到了解决方案。

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

我需要改变我的WPF项目中的一些东西,即.NET Framework 4.7.2. 首先,删除对的引用AspNet.SignalR并将其更改为Core一个。要获得此功能,只需在您的 nuget 中安装.Net Framework proj

在此处输入图片说明

然后,此服务编译时没有错误(idk,如果它可以工作,但我的WebApiwith上的 Connected count 为 1 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我的WPF编译无一例外。我收到来自服务器的通知

  • 这次真是万分感谢。我创建了[WebApi Core to WPF](https://github.com/pwujczyk/ProductivityTools.Examples.SignalR.WebAPI2WPF“SignalR example”)的最简单的示例。 (2认同)