aspire
aspire copied to clipboard
Create `IConfiguration` extension methods to get Aspire convention-based values
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);
}
}
cc @ReubenBond
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.
Where is that, I just looked and can't seem to find what you're describing?
I also recently stumbled upon these, that are similar by don't expand the config names: dotnet/aspire/blob/main/src/Shared/IConfigurationExtensions.cs.