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.

AddHostedService() registers service as Transient in the 2.1.1 apart from the 2.0

See original GitHub issue

Hello.

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 image

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 image 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:

  1. Can it be a problem and why have you changed the lifetime?
  2. 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 the IHostedService 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:44 (21 by maintainers)

github_iconTop GitHub Comments

15reactions
Drawaescommented, Jul 14, 2018

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.

8reactions
davidfowlcommented, Jul 13, 2018

I’m saying don’t implement the interface on the IHostedService itself. Here’s an example of a queue:

public interface IQueue<T>
{
    T Dequeue();
    void Enqueue(T item);
}

public class QueueHostedService : IHostedService
{
     public QueueHostedService(IQueue<WorkItem> queue)
     {
     }

     // Write code to Dequeue from the queue and perform some work
}


public class HomeController : Controller
{
     private readonly IQueue<WorkItem> _queue;
     public HomeController(IQueue<WorkItem> queue)
     { 
          _queue = queue;
     }
     
     [Route("/item")]
     public IActionResult Post(WorkItem item)
     {
           _queue.Enqueue(item);
     }
}

In this example, you could implement the IQueue<WorkItem> on the QueueHostedService, or you can implement it on another type that the QueueHostedService uses.

Read more comments on GitHub >

github_iconTop 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 >

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