liveBook · Manning

Withreference

As explained in Building Distributed Applications that Work

WithReference() is an Aspire API method that adds a reference from one application resource to another.

WithReference() adds a reference from one Aspire application resource to another. In an online shop, the web frontend references the API so it can find the API through service discovery. The API can remain off the public network: the frontend reaches it through Aspire’s internal network. section 1.7.3

builder.AddProject<Projects.OnlineShop_Web>("webfrontend")
    .WithExternalHttpEndpoints()
    .WithHttpHealthCheck("/health")
    .WaitFor(apiService)
    .WithReference(apiService);

The frontend registration combines separate concerns: WithReference(apiService) supplies the API reference, WaitFor(apiService) delays frontend startup until the API is ready, and WithExternalHttpEndpoints() makes the frontend externally accessible. section 1.7.3

Use the same pattern for infrastructure dependencies. The API receives a Keycloak reference so it can resolve the identity provider’s address. A frontend can use a custom WithReference() overload that also places Keycloak’s client secret in the Identity__ClientSecret environment setting for its OIDC flow. section 2.4

public static IResourceBuilder<TResource>
    WithReference<TResource>(
        this IResourceBuilder<TResource> builder,
        IResourceBuilder<KeycloakResource> keycloakBuilder,
        string env)
    where TResource : IResourceWithEnvironment
{
    builder.WithReference(keycloakBuilder);
    builder.WithEnvironment(
        env, keycloakBuilder.Resource.ClientSecret);

    return builder;
}

This custom overload first calls WithReference(keycloakBuilder), then sets the requested environment variable to the Keycloak resource’s client secret. The reference and secret configuration are distinct steps. section 2.4

Referencing a database

Create a SQL Server resource and its sqldb database, then add .WithReference(sqldb) to the API project. Pair it with .WaitFor(sqldb) when the API must not start before the database is ready. The database reference appears alongside SQL Server in the Aspire dashboard. section 3.3

How Building Distributed Applications that Work covers it

How deep it goes
Shows chained resource registrations and a custom overload that also sets an environment variable; does not detail service-discovery internals.
Examples
Uses a frontend-to-API registration, a Keycloak client-secret overload, and an API-to-database registration.
What it assumes
Assumes resources have been registered in an Aspire host before passing their builders to WithReference().
What you can do after
Operate/configure it by adding resource references in Aspire AppHost registrations and using WaitFor() separately when startup order matters.
How it connects
Links service discovery with startup readiness, external HTTP endpoints, OIDC configuration, and SQL Server resources.

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.