aspire icon indicating copy to clipboard operation
aspire copied to clipboard

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

Open IEvangelist opened this issue 1 year ago • 4 comments

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);
    }
}

IEvangelist avatar Feb 29 '24 21:02 IEvangelist

cc @ReubenBond

davidfowl avatar Feb 29 '24 22:02 davidfowl

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.

ReubenBond avatar Mar 14 '24 17:03 ReubenBond

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

IEvangelist avatar Mar 14 '24 23:03 IEvangelist

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

IEvangelist avatar May 22 '24 11:05 IEvangelist