SignalR Core-StatusCode:404,ReasonPhrase:“未找到”,版本:1.1

时间:2019-05-28 16:27:35

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

我有两个项目。

首先,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);
    }

问题从这里开始。

我有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();。从这一行,我得到一个错误:

  

“的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

EDIT2

WPF项目是.NET Framework 4.7.2,而WebApiCore 2.1

1 个答案:

答案 0 :(得分:0)

我相信我找到了解决方案。

在我发现的互联网上寻找它时,对{ "name": "my-app", "version": "1.0.0", "license": "ISC", "dependencies": { "@webcomponents/webcomponentsjs": "^2.2.10", "lit-element": "^2.1.0" } } 的引用无法与基于Core的Microsoft.AspNet.SignalR服务器配合使用。

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

enter image description here

然后,该服务编译没有错误(如果有效,则为idk,但是我的.Net Framework projWebApi的Connected计数为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 }); await hubConnection.StartAsync(); } } 会毫无例外地进行编译,但我不知道它是否可以在该阶段正常工作

编辑

有效。当我从WPF发送通知时,我在自己的WebApi中碰到了hubConnection.On<string>,所以我的回答很好