AddHostedService() registers service as Transient in the 2.1.1 apart from the 2.0
See original GitHub issueHello.
According to the documentation for the 2.0
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.0#timed-background-tasks
We have to register services as Singleton
According to the documentation for the 2.1
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.1#timed-background-tasks
We should use the AddHostedService
extension
which is Transient under the hood now: https://github.com/aspnet/Hosting/blob/master/src/Microsoft.Extensions.Hosting.Abstractions/ServiceCollectionHostedServiceExtensions.cs#L18
So my questions are:
- Can it be a problem and why have you changed the lifetime?
- What should I do If I have one more interface to reach the the service from the DI? Right now I’m doing the following:
Register custom interface as singleton:
services.AddSingleton<ICandleSubscriptionHostedService, CandleSubscriptionHostedService>();
Use the custom resolver for theIHostedService
registration:services.AddSingleton<IHostedService, CandleSubscriptionHostedService>(provider => (CandleSubscriptionHostedService) provider.GetRequiredService<ICandleSubscriptionHostedService>());
Both are Singletons so the second lifetime doesn’t matter a lot actually.
Thank you!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:44 (21 by maintainers)
Top Results From Across the Web
Proper way to register HostedService in ASP.NET Core. ...
Net Core 2.2 and 3.1 the behavior has changed, AddHostedService is now adding a Singleton instead of the previous Transient service.
Read more >AddHostedService() registers service as Transient in the ...
AddHostedService () registers service as Transient in the 2.1.1 apart from the 2.0.
Read more >Use scoped services within a BackgroundService - .NET
The services are registered in (Program. cs). The hosted service is registered with the AddHostedService extension method. For more information ...
Read more >Untitled
NET Core Background Services Implement background tasks using ... NET Core 2.0 AddHostedService() registers service as Transient in the 2.1.1 apart .
Read more >Background tasks with hosted services in ASP.NET Core
The services are registered in IHostBuilder.ConfigureServices ( Program.cs ). The hosted service is registered with the AddHostedService ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Agreed but the naming is not great. I suspect when most people hear service they equate it to Singleton… I sure did and it caught me out.
I’m saying don’t implement the interface on the IHostedService itself. Here’s an example of a queue:
In this example, you could implement the IQueue<WorkItem> on the QueueHostedService, or you can implement it on another type that the QueueHostedService uses.