liveBook · Manning

Waitfor

As explained in Building Distributed Applications that Work

WaitFor() is an Aspire API method that delays a resource's startup until another resource is ready.

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

How Building Distributed Applications that Work covers it

How deep it goes
Explains startup ordering through frontend/API and API/database examples without detailing readiness detection.
Examples
Shows a web frontend waiting for an API and an API waiting for identity-provider and database resources.
What you can do after
Operate resource startup by adding WaitFor() to dependent Aspire resource registrations.
How it connects
Connects service startup dependencies with APIs, frontends, databases, service discovery, and resource references.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Distributed Applications that Work. The section links point to the parts of the book it draws on.