dotnet / aspire

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

Create `IConfiguration` extension methods to get Aspire convention-based values #2549

Open IEvangelist opened 8 months ago

IEvangelist commented 8 months ago

It would be really nice if Aspire added some extension methods on IConfiguration that allow for convenience-based APIs for getting values.

For example, it's ugly to ask the config for "services__serviceName__0". Something like this might be really useful:

public static class ConfigurationExtensions
{
    public static Uri GetServiceHttpUri(this IConfiguration config, string name) =>
        config.GetServiceUri(name, 0);

    public static Uri GetServiceHttpsUri(this IConfiguration config, string name) =>
        config.GetServiceUri(name, 1);

    private static Uri GetServiceUri(this IConfiguration config, string name, int index)
    {
        ArgumentException.ThrowIfNull(name);

        var url = config[$"services:{name}:{index}"];

        return new(url);
    }
}
davidfowl commented 8 months ago

cc @ReubenBond

ReubenBond commented 7 months ago

This can be useful. We added something similar in the Aspire.Hosting.Testing package as an extension on DistributedApplication. The API would need to take an optional endpoint name. I would prefer it returns HTTP only if HTTPS is not available.

IEvangelist commented 7 months ago

Where is that, I just looked and can't seem to find what you're describing?

IEvangelist commented 5 months ago

I also recently stumbled upon these, that are similar by don't expand the config names: dotnet/aspire/blob/main/src/Shared/IConfigurationExtensions.cs.