question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to integrate SignalR when also using IHubContext<> instances

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
dotnetjunkiecommented, Aug 19, 2021

Well I can add services.AddSignalR() But then everything is going over the MS DI. I thought that actually this is not what I should add if I want to be able to have the instances created with SimpleInjector.

You certainly need to call .AddSignalR(). This sets up the proper infrastructure such as the IHubContext<T> registrations. That will not cause everything to go over MS.DI, because will register the SimpleInjectorHubActivator<T> after calling .AddSignalR(). That replaces the default IHubActivator<T>.

I’ll update the documentation accordingly.

0reactions
dotnetjunkiecommented, Aug 19, 2021

That probably means that MyClient is registered inside MS.DI. Try to remove it from the ServiceCollection. But TBH, this will not fully solve the problem, because Simple Injector will try to cross-wire the IHubContext<T> by requesting it from MS.DI which will likely fail as well, because of the absence of that registration.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SignalR HubContext
In ASP.NET Core SignalR, you can access an instance of IHubContext via dependency injection. You can inject an instance of IHubContext into ...
Read more >
Instantiate SignalR Hub Object With IHubContext
You can create a class that has the IHubContext<T> injected in. Inject other dependencies if you want, or resolve the service from controllers ......
Read more >
Build Real-time Applications with ASP.NET Core SignalR
SignalR Service integrates into an existing ASP.NET Core SignalR application using a NuGet package and requires minimal modifications to your ...
Read more >
SignalR Introduction And Implementation Using The .NET ...
In this article, you will learn about SignalR introduction and implementation using the.NET Core 6 Web API and Angular 14.
Read more >
Adding Real-Time Functionality To .NET Applications With ...
To start using SignalR you'll need to: ... Then you need a SignalR Hub , which is the central component in your application...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found