I've been struggling for the past two days on how to get SignalR 2 to work with Web API. I tried to follow a few tutorials, from Microsoft and from others, about the topic, but still took me time to understand where I was wrong. I'm posting this question to help someone else that ends up in the same situation I was on.
I needed to create a Hub on SignalR that could respond to a Console Application (C#) and a Web App (AngularJs) as clients, and I also needed it to send signals after a certain method on API being hit.
I followed these (and others) tutorials:
ASP.NET SignalR Hubs API Guide - Server (C#)
ASP.NET SignalR Hubs API Guide - .NET Client (C#)
Stream Web API Transactions Using SignalR
And some more, but those are the best I could find. Also, a dozen of others questions, here and on other sites.
The best I could come up with was a solution that only answered to the API method being hit, but the clients wasn't able to fire any of the Hub's methods (they could only listen to it).
This was my Hub's code:
public class MyHub : Hub
{
private static IHubContext hubContext =
GlobalHost.ConnectionManager.GetHubContext<MyHub>();
public static void GetStatus(string message)
{
hubContext.Clients.All.acknowledgeMessage($"GetStatus: {message}");
}
public void GetMessage(string message)
{
hubContext.Clients.User(id).acknowledgeMessage($"GetStatus: {message}");
}
}
And my question was: How can I make the clients hit those methods on Hub, so they can get some personal response, if needed?