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
