I’m trying to use SignalR to send an update through a websocket when a request is made to an endpoint. But, I’m struggling to inject the relevant Controller with a hub dependency. I’ve got a controller with the following GET function:
[HttpGet]
public IEnumerable<Zaal> GetZaal()
{
new BoekingUpdateHub().SendData("Test");
foreach(Stoel s in _context.Stoel)
{
Console.WriteLine(s.Id);
}
return _context.Zaal;
}
Problem is, when I try to use this endpoint, the hub is not instantiated properly, as
public class BoekingUpdateHub : Hub
{
public async Task SendData(string data)
{
await Clients.All.SendAsync("ReceiveData", data);
}
}
Gives me a null reference exception on the await Clients.All, as Clients was null
I’ve tried using dependency injection to resolve this problem, but it gives me:
System.InvalidOperationException: Unable to resolve service for type
‘WDPR.Hubs.BoekingUpdateHub’ while attempting to activate
‘WDPR.Controllers.ZaalController’.
I’ve looked around and I can’t find a solution, I’m completely lost on how to implement this properly.
2
Answers
I figured it out. Rather than putting the Hub as an argument in the constructor, you add IHubContext as an argument, in this case the T is the Hub you want to actually use (in my case the hub is called MyHub)
Then, you can create the Hub inside one of the Endpoint functions:
Finally, make sure the IHubContext is an argument in the Hub's constructor:
Now, notice we're using the IHubContext as a source for our Clients, this prevents the Clients from being null. Doing these steps allows you to send information through a SignalR websocket when an endpoint receives a request.
Configure the controller as follows