3

I have a .Net core (v2.2) API. That API uses some external service calls. We want to make them as fire and forget calls. We used .net core Background service to implement that. We have multiple background services. Now if I register all those IHostedService in Dependency Injection, The last registered background service works, and others come as null in constructor injection of project. Registering dependencies like:

services.AddHostedService<BackgroundServiceA>();
 services.AddHostedService<BackgroundServiceB>();

And also, I tried adding them as:

 services.AddSingleton<IHostedService, BackgroundServiceA>();
 services.AddSingleton<IHostedService, BackgroundServiceB>();

In both the cases, only BackgroundServiceB works, BackgroundServiceA comes as null in constructor injection.

To handle this, I was using ServiceProvider to get the object.

  var services = serviceProvider.GetServices<IHostedService>();
 _backgroundServiceA = services.First(o => o.GetType() == typeof(BackgroundServiceA)) as BackgroundServiceA;

Is it a good way to handle such an issue or I am missing something while registering dependencies.

Also, can we trigger both the background calls parallel?

3 Answers 3

5

(edit, I initially answered the wrong question....)

Dependency injection doesn't work with multiple implementations of the same interface. Since each hosted service is registered as an implementation of IHostedService, only one service can be easily injected into other types.

However you can register each service again, with a factory method to locate the background singleton instance;

services.AddHostedService<BackgroundServiceA>();
services.AddHostedService<BackgroundServiceB>();
services.AddSingleton<BackgroundServiceA>(p => p.GetServices<IHostedService>().OfType<BackgroundServiceA>().Single());
services.AddSingleton<BackgroundServiceB>(p => p.GetServices<IHostedService>().OfType<BackgroundServiceB>().Single());

Edit: It's probably better to implement this the other way around;

services.AddSingleton<BackgroundServiceA>();
services.AddSingleton<IHostedService>(p => p.GetService<BackgroundServiceA>());
1
  • If we set it as a singleton and if there are some variables in the background process methods that contain customer-specific values, will it impact that on multi-tenant application?
    – Sahil
    Commented May 7, 2020 at 7:58
2

Try to register them via AddHostedService call in separate ConfigureServices call outside startup, as stated in docs:

public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); }

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        })
        .ConfigureServices(services =>
        {
            services.AddHostedService<BackgroundServiceA>();
            services.AddHostedService<BackgroundServiceB>();
        });
1
  • 1
    I updated the question. I tried adding them as AddHostedService also. It did not work.
    – Sahil
    Commented May 7, 2020 at 5:12
-1

avoid direct access to hosted service as discussed here

use intermediate service instead as describe in 'Queued background tasks' example

1
  • It is not a Queued background service, It is a simple Background service, where we override the ExecucteAsync Method.
    – Sahil
    Commented May 7, 2020 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.