Wait for bootstrap in your code
How to integrate with IBootstrapService
If you have any Hosted Services in your code - you'd probably want to wait until your service is in the consistent state.
Here's how you can do this:
Inject
IBootstrapServiceinto your service via DI.Call any of the methods to wait for bootstrap to happen.
There are 4 methods available in total, depending on your coding style:
Method | Description |
|---|---|
| Wait and return current server's state. |
| Wait and return |
| Wait and throw an |
| Subscribe for state changes. Callback will only be called once! |
Full example
public class DependentService(ILogger<DependentService> logger, IBootstrapService bootstrapService) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
logger.LogInformation("Waiting until bootstrap actions are done..");
// Inline assert with exception:
// await bootstrapService.AssertBootstrapSuccessful(stoppingToken);
// Exception-less check:
if (!await bootstrapService.IsBootstrapSuccessful(stoppingToken))
{
logger.LogError("Bootstrap has failed.");
return;
}
// Explicit check:
// if(await bootstrapService.WaitForBootstrap(stoppingToken) == BootstrapState.Successful)
logger.LogInformation("Bootstrap actions are done.");
}
}
02 November 2025