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

2024-01-06

我有两个项目。

First, WebApi包含用于使用的集线器SignalR:

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

我将 Hub 设置为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:

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

问题从这里开始。

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

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

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

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

“状态代码:404,原因短语:'未找到',版本:1.1,内容:System.Net.Http.StreamContent,标头:X-SourceFiles:=?UTF-8?B?QzpcVXNlcnNcQWRtaW5cc291cmNlXHJlcG9zXFRhc2tNYW5hZ2VyXFRhc2tNYW5hZ2VyLXdlYmFwaV xOb3RpZmljYXRpb25zXHNpZ25hbHJcbmVnb3RpYXRl?= 日期:2019 年 5 月 28 日,星期二16:25:13 GMT 服务器:Kestrel X 供电者:ASP.NET 内容长度:0

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

EDIT

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

需要连接 ID

EDIT2

WPF项目是.NET Framework 4.7.2 and WebApi is Core 2.1.


我找到了一个解决方案。

在互联网上寻找它我发现,参考Microsoft.AspNet.SignalR不会与SignalR基于Core的服务器。

我需要改变我的一些事情WPF项目是.NET Framework 4.7.2。首先,删除引用AspNet.SignalR并将其更改为Core一。要获得此功能,只需将这个 nuget 安装到您的.Net Framework proj:

然后,该服务编译时没有错误(我不知道它是否有效,但我的连接计数为 1)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编译无异常。我从服务器收到通知

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐