Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to register HostedService in ASP.NET Core. AddHostedService vs AddSingleton

What is the proper way to register a custom hosted service in ASP.NET Core 2.1? For example, I have a custom hosted service derived from BackgroundService named MyHostedService. How should I register it?

public IServiceProvider ConfigureServices(IServiceCollection services) {                //...     services.AddSingleton<IHostedService, MyHostedService>(); } 

or

public IServiceProvider ConfigureServices(IServiceCollection services) {                //...     services.AddHostedService<MyHostedService>(); } 

?

Here we can see the first case, but here there is a second case.

Are these methods equal?

like image 477
Denis Babarykin Avatar asked Jul 23 '18 13:07

Denis Babarykin


People also ask

Is IHostedService a singleton?

When you register implementations of IHostedService using any of the AddHostedService extension methods - the service is registered as a singleton. There may be scenarios where you'd like to rely on a scoped service. For more information, see Dependency injection in .

When should I use IHostedService?

The IHostedService interface provides a convenient way to start background tasks in an ASP.NET Core web application (in . NET Core 2.0 and later versions) or in any process/host (starting in . NET Core 2.1 with IHost ).


1 Answers

Update

In the past, a HostedService was a long-lived transient, effectively acting as a singleton. Since .NET Core 3.1 it's an actual Singleton.


Use AddHostedService

A hosted service is more than just a singleton service. The runtime "knows" about it, can tell it to start by calling StartAsync or stop by calling StopAsync() whenever eg the application pool is recycled. The runtime can wait for the hosted service to finish before the web application itself terminates.

As the documentation explains a scoped service can be consumed by creating a scope inside the hosted service's worker method. The same holds for transient services.

To do so, an IServicesProvider or an IServiceScopeFactory has to be injected in the hosted service's constructor and used to create the scope.

Borrowing from the docs, the service's constructor and worker method can look like this:

public IServiceProvider Services { get; }  public ConsumeScopedServiceHostedService(IServiceProvider services,      ILogger<ConsumeScopedServiceHostedService> logger) {     Services = services;     _logger = logger; }   private void DoWork() {     using (var scope = Services.CreateScope())     {         var scopedProcessingService =              scope.ServiceProvider                 .GetRequiredService<IScopedProcessingService>();          scopedProcessingService.DoWork();     } } 

This related question shows how to use a transient DbContext in a hosted service:

public class MyHostedService : IHostedService {     private readonly IServiceScopeFactory scopeFactory;      public MyHostedService(IServiceScopeFactory scopeFactory)     {         this.scopeFactory = scopeFactory;     }      public void DoWork()     {         using (var scope = scopeFactory.CreateScope())         {             var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();             …         }     }     … } 
like image 193
Panagiotis Kanavos Avatar answered Sep 24 '22 04:09

Panagiotis Kanavos