【问题标题】:SignalR Core - StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1SignalR 核心 - 状态代码:404,ReasonPhrase:“未找到”,版本:1.1
【发布时间】:2019-05-28 16:27:35
【问题描述】:

我有两个项目。

首先,WebApi 包含用于使用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 中发出这样的通知:

    [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 构造函数中初始化它:

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

问题始于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 项目中连接到 Hub?

编辑

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

需要连接 ID

EDIT2

WPF 项目是 .NET Framework 4.7.2WebApiCore 2.1

【问题讨论】:

    标签: c# asp.net-core signalr asp.net-core-2.1


    【解决方案1】:

    我找到了解决办法。

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

    我需要更改我的WPF 项目中的一些内容,即.NET Framework 4.7.2。首先,删除对AspNet.SignalR的引用,改成Core一个。要得到这个,只需在你的 .Net Framework proj 中安装这个 nugets:

    然后,此服务编译时不会出现错误(如果它还可以工作,idk,但我在 WebApiCore SignalR 上将 Connected count 设为 1):

    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 编译无异常。我从服务器收到通知

    【讨论】:

    • 谢谢你。我创建了WebApi Core to WPF 的最简单示例。
    • @PawelWujczyk 你可以做到even better :)。那里有 WebApi,CQRS 模式作为服务器,WPF 作为客户端
    • @michasaucer 你的例子并不好。这非常复杂而且离题。 Pawel 的演示确实是最简单的例子。
    猜你喜欢
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 2019-03-15
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多