Advertisement
Intermediate ASP.NET Core, Background Tasks & Hosted Services

How do multiple independently-registered BackgroundServices coexist within the same application, and does registration order matter?

Short answer

Each BackgroundService is registered independently via AddHostedService (), and the host starts each one's StartAsync sequentially in registration order (so a service registered first completes its StartAsync before the next one's begins), but their ExecuteAsync work loops then…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Each BackgroundService is registered independently via AddHostedService<T>(), and the host starts each one's StartAsync sequentially in registration order (so a service registered first completes its StartAsync before the next one's begins), but their ExecuteAsync work loops then run concurrently, independently, for the rest of the application's lifetime -- they don't interfere with each other unless they explicitly share state or resources.
builder.Services.AddHostedService<DatabaseMigrationService>();  // StartAsync runs first, must complete
builder.Services.AddHostedService<QueueProcessor>();             // then this one starts
builder.Services.AddHostedService<CacheWarmupService>();         // then this one
// After startup, all three ExecuteAsync loops run concurrently
Real-world example
A team ensures database migrations complete before any other background work begins by registering a DatabaseMigrationService first, relying on the host's sequential StartAsync ordering to enforce this critical startup dependency.
Likely follow-ups
  • What happens if one service's StartAsync throws an exception during startup?
  • How would you make services start concurrently instead of sequentially?
Related topics