Multiple BackgroundService or IHostedServices but Only One Works
In my worker app, I attempted to add multiple hosted services as follow:
builder.Services
.addSingleton(HostedService1)
.addSingleton(HostedService2)
.addSingleton(HostedService3);
All the hosted services are added, but when the application run, only 1 is executing.
Thanks to Stephen Cleary, apparently issue with synchronous call. https://blog.stephencleary.com/2020/05/backgroundservice-gotcha-startup.html.
I ended up using Task.Run for code that executes for a long time. Inside the ExecuteAsync:
await Task.Run(async () => await LongRunningProcess());
Comments
Post a Comment