dotnet / aspire

Tools, templates, and packages to accelerate building observable, production-ready apps
https://learn.microsoft.com/dotnet/aspire
MIT License
3.84k stars 461 forks source link

Feature Request: AppHost should have support for global configuration and environment variables #5502

Open samsp-msft opened 2 months ago

samsp-msft commented 2 months ago

Background

In .NET 8 we added support for specifying the metrics you want exposed using config. We have not done much with the feature today.

Currently its less usable in Aspire than adding them in code, because with code you can do it once in ServiceDefaults, and it will apply to every project.

If I want to do it with config files, then I need to put it into the config file for each project in the Aspire solution.

Similarly if I want to apply an environment variable to each project - like our favorite Application Insights connection string, then I need to add it to each project in the AppHost.

Feature Request

davidfowl commented 2 months ago

I think this is the same as https://github.com/dotnet/aspire/issues/1053

DamianEdwards commented 2 months ago

The appsettings.json part of this is indeed #1053.

For the environment variables part, it would be pretty easy to create an extension method that adds an environment variable to all project/container resources in the application model (or based on a predicate) using a lifecycle hook.

davidfowl commented 2 months ago

using a lifecycle hook.

Even easier in 8.2 using the new eventing model!

#pragma warning disable ASPIREEVENTING001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

builder.Eventing.Subscribe<BeforeStartEvent>((data, token) =>
{
    foreach (var service in data.Model.Resources)
    {
    }
    return Task.CompletedTask;
});