I'm new to signalR. What I'd like to do if it's possible is to inject an instance of a hub class into a web api controller. I've tried doing it and always get the error that my hub can't be created outside the signalR pipeline. The workaround that I've found
is obtaining the IHubContext and also found a controller class that extends WebApiController by adding IHubContext property, but I'd like to know if it's possible to fully decouple the web api controller from signalR.
<div class="line" id="file-webapi_signalr-LC20">
public interface ISomeNotifier
{
void Notify(SomeEvent someEvent);
}
public class SomeHub : Hub, ISomeNotifier
{
public void Notify(SomeEvent someEvent)
{
Clients.All.somethingHappened(someEvent);
}
}
public class MyController : ApiController
{
public MyController(ISomeNotifier notifier)
{
//initialize private var
}
}
</div> <div class="line">Thanks in advance.</div> <div class="line">Cristian</div>
While you may not be able to inject a working hub itself, you can create an IHubContext which will allow you to message the clients connected to the specified Hub.
None
0 Points
1 Post
Injecting Hub instance into web api controller
May 29, 2014 06:48 AM|CristianSM|LINK
Hi All,
I'm new to signalR. What I'd like to do if it's possible is to inject an instance of a hub class into a web api controller. I've tried doing it and always get the error that my hub can't be created outside the signalR pipeline. The workaround that I've found is obtaining the IHubContext and also found a controller class that extends WebApiController by adding IHubContext property, but I'd like to know if it's possible to fully decouple the web api controller from signalR.
<div class="line" id="file-webapi_signalr-LC20"> </div> <div class="line">Thanks in advance.</div> <div class="line">Cristian</div>Member
100 Points
20 Posts
Microsoft
Re: Injecting Hub instance into web api controller
Jun 03, 2014 07:10 PM|halter73|LINK
While you may not be able to inject a working hub itself, you can create an IHubContext which will allow you to message the clients connected to the specified Hub.
You can learn more about create and use an IHubContext from SignalR Hubs API Guide.