How to integrate SignalR when also using IHubContext<> instances
See original GitHub issueI would like to ask a question that refers to the SignalR Core integration.
With SignalR Hub instances are created when messages are received from clients. But if now the server want to send a message outside of such a “message receiving scope” you can use an instance of IHubContext<MyHub> hubContext. Se for example this page for a description what I am talking about.
Now I have such an application with a HubContext and when starting it crashes stating it is unable to resolve IHubContext<MyHub> hubContext.
System.InvalidOperationException: 'Error while validating the service descriptor ‘ServiceType: RamphastosUi.Web.IRamphastosViewClientRemote Lifetime: Singleton ImplementationType: Example.MyClient’: Unable to resolve service for type ‘Microsoft.AspNetCore.SignalR.IHubContext`1[Example.MyHub]’ while attempting to activate ‘Example.MyClient’.
When you have a class where you inject this HubContext:
public class MyClient
{
public MyClient(IHubContext<MyHub> hubContext){
{
//...
}
}
What do I need to do to make this case also work with SimpleInjector?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5

Top Related StackOverflow Question
You certainly need to call
.AddSignalR(). This sets up the proper infrastructure such as theIHubContext<T>registrations. That will not cause everything to go over MS.DI, because will register theSimpleInjectorHubActivator<T>after calling.AddSignalR(). That replaces the defaultIHubActivator<T>.I’ll update the documentation accordingly.
That probably means that
MyClientis registered inside MS.DI. Try to remove it from theServiceCollection. But TBH, this will not fully solve the problem, because Simple Injector will try to cross-wire theIHubContext<T>by requesting it from MS.DI which will likely fail as well, because of the absence of that registration.