dotnet / aspire

An opinionated, cloud ready stack for building observable, production ready, distributed applications in .NET
https://learn.microsoft.com/dotnet/aspire
MIT License
2.98k stars 280 forks source link

Add overload to WithEnvironment for ReferenceExpression #3996

Closed andi0b closed 1 week ago

andi0b commented 2 weeks ago

This adds an overload to resource.WithEnvironment to accept RefrerenceExpression. With this API change it's more obvious how to pass a ReferenceExpresson,

This was suggested by @davidfowl in https://github.com/dotnet/aspire/issues/2111#issuecomment-2080354270

Example before this PR:

builder.AddNpmApp(...)
    .WithEnvironment(context => context.EnvironmentVariables["DB_CONNECTION_URL"] = db.Resource.GetConnectionUrl());

after:

builder.AddNpmApp(...)
    .WithEnvironment("DB_CONNECTION_URL", db.Resource.GetConnectionUrl());

In this example GetConnectionUrl() is an extension method that builds a custom connection URL and returns ReferenceExpression, see full example: https://github.com/dotnet/aspire/issues/2111#issuecomment-2080238547

Microsoft Reviewers: Open in CodeFlow
andi0b commented 2 weeks ago

@dotnet-policy-service agree

davidfowl commented 1 week ago

/backport to release/8.0

github-actions[bot] commented 1 week ago

Started backporting to release/8.0: https://github.com/dotnet/aspire/actions/runs/8905408380

davidfowl commented 1 week ago

Thanks!