I'm trying to create background services using IHostedService. Everything works fine if I only have ONE background service. When I try to create more than one implementation of IHostedService
only the one that was registered first actually runs.
services.AddSingleton<IHostedService, HostedServiceOne>();
services.AddSingleton<IHostedService, HostedServiceTwo>();
In the above sample StartAsync
on HostedServiceOne
gets called but StartAsync
on HostedServiceTwo
never gets called. If I swap the order of registering the two implementations of IHostedService
(put IHostedServiceTwo
before IHostedServiceOne
) then StartAsync
on HostedServiceTwo
gets called but never for HostedServiceOne
.
EDIT:
I was directed to the following:
How to register multiple implementations of the same interface in Asp.Net Core?
However this isn't for IHostedService
. To use the suggested approach I would have to make a call to serviceProvider.GetServices<IService>();
but it seems that IHostedService.StartAsync
seems to be called internally. I'm not even sure where I would call that to trigger IHostedService.StartAsync
.
I had the same problem. It was necessary to return Task.CompletedTask in each services;
public class MyHostedService: IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
{
Task.Run(() => SomeInfinityProcess(cancellationToken));
return Task.CompletedTask;
}
public void SomeInfinityProcess(CancellationToken cancellationToken)
{
for (; ; )
{
Thread.Sleep(1000);
if (cancellationToken.IsCancellationRequested)
break;
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
Startup.cs is same:
services.AddHostedService<MyHostedService>();
services.AddHostedService<MyHostedService2>();
...
Register your HostedService
as below :
// services.AddSingleton<IHostedService, HostedServiceOne>();
// services.AddSingleton<IHostedService, HostedServiceTwo>();
services.AddHostedService<HostedServiceOne>();
services.AddHostedService<HostedServiceTwo>();
[Update]:
See comments below by @nickvane :
It's because the first service registered is not returning a Task on the StartAsync method so the runtime is waiting and doesn't execute the StartAsync of the next registered hostedservice instance
It's likely the first StartAsync()
didn't finish
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With