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

Popular posts from this blog

AWS EC2 Can't Reach EC2 Metadata Service After Subnet Change

A2 Hosting with .NET Core 2.1

Xcode CodeSign Incorrectly States Password is Incorrect