SignalR Core - StatusCode:404,ReasonPhrase:'Not Found',版本:1.1
SignalR Core - StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1
我有两个项目。
首先,WebApi
包含使用 SignalR
的集线器:
public class NotificationsHub : Hub
{
public async Task GetUpdateForServer(string call)
{
await this.Clients.Caller.SendAsync("ReciveServerUpdate", call);
}
}
我在 Startup.cs
中设置了 Hub:
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: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcQWRtaW5cc291cmNlXHJlcG9zXFRhc2tNYW5hZ2VyXFRhc2tNYW5hZ2VyLXdlYmFwaVxOb3RpZmljYXRpb25zXHNpZ25hbHJcbmVnb3RpYXRl?= Date: Tue, 28 May 2019 16:25:13 GMT Server: Kestrel X-Powered-By: ASP.NET Content-Length: 0
我的问题是,我做错了什么以及如何连接到我的 WebApi
项目中的 Hub?
编辑
集线器似乎可以正常工作。我在浏览器中输入:https://localhost:44365/notifications
,然后收到消息:
Connection ID required
EDIT2
WPF
项目是 .NET Framework 4.7.2
,WebApi
是 Core 2.1
。
我找到了解决办法。
我在互联网上查找时发现,对 Microsoft.AspNet.SignalR
的引用无法与基于 Core 的 SignalR
服务器一起使用。
我需要更改 WPF
项目中的一些内容,即 .NET Framework 4.7.2
。首先,删除对 AspNet.SignalR
的引用并将其更改为 Core
一个。为此,只需在 .Net Framework proj
:
中安装此 nugets
然后,此服务编译无误(idk 如果它还有效,但我的 WebApi
和 Core SignalR
的连接计数为 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
包含使用 SignalR
的集线器:
public class NotificationsHub : Hub
{
public async Task GetUpdateForServer(string call)
{
await this.Clients.Caller.SendAsync("ReciveServerUpdate", call);
}
}
我在 Startup.cs
中设置了 Hub:
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: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcQWRtaW5cc291cmNlXHJlcG9zXFRhc2tNYW5hZ2VyXFRhc2tNYW5hZ2VyLXdlYmFwaVxOb3RpZmljYXRpb25zXHNpZ25hbHJcbmVnb3RpYXRl?= Date: Tue, 28 May 2019 16:25:13 GMT Server: Kestrel X-Powered-By: ASP.NET Content-Length: 0
我的问题是,我做错了什么以及如何连接到我的 WebApi
项目中的 Hub?
编辑
集线器似乎可以正常工作。我在浏览器中输入:https://localhost:44365/notifications
,然后收到消息:
Connection ID required
EDIT2
WPF
项目是 .NET Framework 4.7.2
,WebApi
是 Core 2.1
。
我找到了解决办法。
我在互联网上查找时发现,对 Microsoft.AspNet.SignalR
的引用无法与基于 Core 的 SignalR
服务器一起使用。
我需要更改 WPF
项目中的一些内容,即 .NET Framework 4.7.2
。首先,删除对 AspNet.SignalR
的引用并将其更改为 Core
一个。为此,只需在 .Net Framework proj
:
然后,此服务编译无误(idk 如果它还有效,但我的 WebApi
和 Core SignalR
的连接计数为 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
编译无一例外。我从服务器