-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do I correctly inject an IHostedService into a Controller? #596
Comments
That's because your constructor references Because Simple Injector can't find So fix this, you can do 2 things:
|
Right, it's obvious now that you say it 😄 Thank you! I've changed the registration as follows, if I understand correctly then the background service must be registered as an services.AddSingleton<ActorSystemBackgroundService>();
services.AddSingleton<IHostedService, ActorSystemBackgroundService>(
serviceProvider => serviceProvider.GetService<ActorSystemBackgroundService>()); |
So why does your |
There could be multiple Looking at |
btw, have you seen this related q/a? https://stackoverflow.com/questions/50394666/injecting-simple-injector-components-into-ihostedservice-with-asp-net-core-2-0 |
I hadn't seen it yet. I did see you talk somewhere else about creating an adapter, but didn't quite understand it. Thanks for the link, I'll see if I can make it fit with my requirements. |
Or I would suggest this post https://stackoverflow.com/questions/51254053/how-to-inject-a-reference-to-a-specific-ihostedservice-implementation I have more than one hosted service and followed that post to inject specific hosted service into controller. |
I'm using ASP.NET Core 2.1 and I'm running into the issue described at https://simpleinjector.readthedocs.io/en/latest/disposabletransientcomponent.html
I have a service implementing BackgroundService. It is registered with
services.AddSingleton<IHostedService, ActorSystemBackgroundService>()
. When trying to add it to the constructor of a controller, e.g.public AutomatedTestsController(ActorSystemBackgroundService actorSystemBackgroundService)
, I get the following error:I don't understand why it says
ActorSystemBackgroundService
is registered as transient, I'm registering it as a singleton. How do I correctly resolve the error in this case?The text was updated successfully, but these errors were encountered: