WaitFor() adds a startup dependency between Aspire resources: the dependent service starts only after the resource it relies on is ready. This is useful when a frontend needs an API, or an API needs a database, before its functionality is available. Pair it with WithReference() when the dependent service also needs a reference to the resource. section 1.7.3 section 3.3
Common dependency relationships include:
- Make the web frontend wait for the API service it calls. section 1.7.3
- Make the API wait for its database and identity-provider resources. section 3.3
builder.AddProject<Projects.OnlineShop_Web>("webfrontend")
.WithExternalHttpEndpoints()
.WithHttpHealthCheck("/health")
.WaitFor(apiService)
.WithReference(apiService);
The webfrontend registration places WaitFor(apiService) before WithReference(apiService). section 1.7.3
var sql = builder.AddSqlServer("sql").WithLifetime(ContainerLifetime.Persistent);
var sqldb = sql.AddDatabase("sqldb");
var apiService = builder
.AddProject<Projects.OnlineShop_ApiService>("apiservice")
.WithHttpHealthCheck("/health")
.WithReference(idp)
.WaitFor(idp)
.WaitFor(sqldb)
.WithReference(sqldb);
The API registration waits for the identity provider and database, then adds a database reference. section 3.3
