69

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?

sajadre
  • 1,141
  • 2
  • 15
  • 30
Denis Babarykin
  • 805
  • 1
  • 6
  • 12
  • 1
    more recent docs recommended the seconds option – Nkosi Jul 23 '18 at 13:53
  • 2
    `Are they equal` the seconds one call the first one internally but as Transient, not singleton – Nkosi Jul 23 '18 at 13:54
  • 3
    It's more than which one is singleton and which isn't. Hosted services get special treatment from the runtime through their `StartAsync`, `StopAsync` methods. Using scoped/transient objects *is* possible by using scopes – Panagiotis Kanavos Jul 23 '18 at 14:26

3 Answers3

61

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>();
            …
        }
    }
    …
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 8
    Minor correction: a hosted service is not, in fact, a singleton service. You can call AddHostedService as many times as you want for some concrete class. The runtime will start up multiple instances of the service. This can be used with BackgroundService to set up a worker pool, for instance. – kayjtea Feb 12 '19 at 16:51
  • @kayjtea I wasn't implying it is, I was answering the question. But it looks like hosted services *are* singletons now – Panagiotis Kanavos Jan 19 '21 at 08:47
  • Does it work the same now? Will it be possible to do it through Factory and ActivatorUtilites.CreateInstance? – xSx Jun 16 '21 at 14:13
32

Update

Somewhere between .Net Core 2.2 and 3.1 the behavior has changed, AddHostedService is now adding a Singleton instead of the previous Transient service. Credit - Comment by LeonG

public static class ServiceCollectionHostedServiceExtensions
{
    /// <summary>
    /// Add an <see cref="IHostedService"/> registration for the given type.
    /// </summary>
    /// <typeparam name="THostedService">An <see cref="IHostedService"/> to register.</typeparam>
    /// <param name="services">The <see cref="IServiceCollection"/> to register with.</param>
    /// <returns>The original <see cref="IServiceCollection"/>.</returns>
    public static IServiceCollection AddHostedService<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THostedService>(this IServiceCollection services)
        where THostedService : class, IHostedService
    {
        services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, THostedService>());

        return services;
    }

    /// <summary>
    /// Add an <see cref="IHostedService"/> registration for the given type.
    /// </summary>
    /// <typeparam name="THostedService">An <see cref="IHostedService"/> to register.</typeparam>
    /// <param name="services">The <see cref="IServiceCollection"/> to register with.</param>
    /// <param name="implementationFactory">A factory to create new instances of the service implementation.</param>
    /// <returns>The original <see cref="IServiceCollection"/>.</returns>
    public static IServiceCollection AddHostedService<THostedService>(this IServiceCollection services, Func<IServiceProvider, THostedService> implementationFactory)
        where THostedService : class, IHostedService
    {
        services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService>(implementationFactory));

        return services;
    }
}

Reference ServiceCollectionHostedServiceExtensions


Original Answer

They are similar but not completely

AddHostedService is part of Microsoft.Extensions.Hosting.Abstractions.

It belongs to Microsoft.Extensions.Hosting.Abstractions in the ServiceCollectionHostedServiceExtensions class

using Microsoft.Extensions.Hosting;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class ServiceCollectionHostedServiceExtensions
    {
        /// <summary>
        /// Add an <see cref="IHostedService"/> registration for the given type.
        /// </summary>
        /// <typeparam name="THostedService">An <see cref="IHostedService"/> to register.</typeparam>
        /// <param name="services">The <see cref="IServiceCollection"/> to register with.</param>
        /// <returns>The original <see cref="IServiceCollection"/>.</returns>
        public static IServiceCollection AddHostedService<THostedService>(this IServiceCollection services)
            where THostedService : class, IHostedService
            => services.AddTransient<IHostedService, THostedService>();
    }
}

Note it is using Transient life time scope and not Singleton

Internally the framework add all the hosted services to another service (HostedServiceExecutor)

public HostedServiceExecutor(ILogger<HostedServiceExecutor> logger, 
    IEnumerable<IHostedService> services) //<<-- note services collection
{
    _logger = logger;
    _services = services;
}

at startup that is a singleton via the WebHost Constructor.

_applicationServiceCollection.AddSingleton<HostedServiceExecutor>();
Pang
  • 9,564
  • 146
  • 81
  • 122
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • 1
    It means, that despite the fact that the service is registered as `scoped` inside `AddHostedService`, his lifetime will be as long, as application lifetime, right? – Denis Babarykin Jul 23 '18 at 14:05
  • @Nkosi, how are transient services added to a singleton, shouldn't the Dependency injector throw in that case? – johnny 5 Jul 23 '18 at 14:06
  • 3
    @DenisBabarykin yes that would be accurate. – Nkosi Jul 23 '18 at 14:09
  • @johnny5 no not necessarily. The transient will still get resolved. Just once by the singleton. :) [`Transient lifetime services are created each time they're requested`](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1#service-lifetimes-and-registration-options) – Nkosi Jul 23 '18 at 14:11
  • @Nkosi are they manually requesting the service, I thought the behavior changed between [.Net-Core 1 and .Net-Core 2](https://stackoverflow.com/a/45811225/1938988) – johnny 5 Jul 23 '18 at 14:24
  • 4
    @johnny5 scoped is special. a singleton can still call a transient lifetime dependency. Technically the transient becomes a singleton itself as the instance will live in the singleton. – Nkosi Jul 23 '18 at 14:27
  • @Nkosi, Oh it's only for scoped, Thanks for that clarification I thought it was all lifetimes – johnny 5 Jul 23 '18 at 14:28
  • @johnny5 scoped lifetime only lasts as long as the lifetime of the request. That is why it is treated differently. – Nkosi Jul 23 '18 at 14:29
  • 6
    Somewhere between .Net Core 2.2 and 3.1 the behavior has changed, AddHostedService is now adding a Singleton instead of the previous Transient service. – LeonG Jun 10 '20 at 10:56
  • so if lifetime of a backgroundservice registered by AddHostedService() is effectively a singletion (which I can approve using net.core 5), what is the actual difference to a singleton service? – yBother Jan 07 '22 at 17:06
14

One huge difference is that AddSingleton() is lazy while AddHostedService() is eager.

A service added with AddSingleton() will be instantiated the first time it is injected into a class constructor. This is fine for most services, but if it really is a background service you want, you probably want it to start right away.

A service added with AddHostedService() will be instantiated immediately, even if no other class will ever want it injected into its constructor. This is typical for background services, that run all the time.

Also, it seems that you cannot inject a service added with AddHostedService() into another class.

Christian Davén
  • 16,713
  • 12
  • 64
  • 77
  • Your last sentence was key for me. I find it rather inconvenient that you can't DI a hosted service into another class! – Erik Westwig May 31 '23 at 19:46
  • 1
    @ErikWestwig You just have to wrap it in a scope. Include `IServiceProvider services` to constructor and `services.CreateScope` – David Graham Jun 11 '23 at 01:20
  • @DavidGraham, can you elaborate? I'm experiencing the same frustration as Erik. I want to create a master service that governs application lifecycle (hence must be derived from IHostedService and added with AddHostedService) but is also available to controllers (hence must be derived from IMasterService and added with AddSingleton). – stephen Jun 16 '23 at 05:52
  • @stephen How about adding the service as you would with `AddHostedService` but create an interface that you can get from the scope. Your controllers can have access to the `IWorkerController`. You create that controller interface to do what you need but if you inject it into the worker and you controllers they all can use it. – David Graham Jun 17 '23 at 00:42