I have two projects.
First, WebApi
that contains Hub for using SignalR
:
public class NotificationsHub : Hub
{
public async Task GetUpdateForServer(string call)
{
await this.Clients.Caller.SendAsync("ReciveServerUpdate", call);
}
}
I set up that Hub in 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"));
}
I belive that i would make notifications like this in 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);
}
The problem starts here.
I have WPF
app that contains 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();
}
}
And i initialize it in my MainViewModel
constructor:
public MainWindowViewModel()
{
HubService.Instance.Initialized();
}
The problem starts in await hubConnection.Start();
. From this line, i get an error:
"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
My question is, what im doing wrong and how to connect to Hub in my WebApi
project?
EDIT
Hub seems to work. I typed into my browser: https://localhost:44365/notifications
and i got message:
Connection ID required
EDIT2
WPF
project is .NET Framework 4.7.2
and WebApi
is Core 2.1
.