dotnet / aspire

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

Aspire.Hosting does not correctly set service discovery configuration for endpoints #2520

Closed DamianEdwards closed 6 months ago

DamianEdwards commented 6 months ago

I noticed this a while back but forgot to log it and now just noticed it again when explicitly trying to use named endpoints.

When referencing one project resource from another for the purposes of service discovery in an Aspire AppHost project, Aspire.Hosting injects configuration values (via environment variables) into the referencing project so that service discovery can be configured. The key names are being set correctly, but the values aren't correctly encoding the endpoint name into the URI host name.

In the following example, this project resource is referencing another project resource named "apiservice" which has two endpoints: "http" and "internalhealthchecks". That should result in three config values being set, 2 for the endpoint named "http" (1 using the compact form and 1 using the expanded form), and 1 for the endpoint named "internalhealthchecks" (1 using the expanded form). Instead, we're only getting 1 entry for the "http" endpoint (it's missing the expanded form entry), and 1 for the "internalhealthchecks" endpoint that's using the compact form which is just wrong.

What I see

image

What it should be

Name Value
servicesapiservice0 http://localhost:5301
servicesapiservice1 http://_http.localhost:5301
servicesapiservice2 http://_internalhealthchecks.localhost:61890
GarretWayne commented 6 months ago

We found the same issue, and it seems to be related to this part in ResourceBuilderExtensions.cs/ CreateEndpointReferenceEnvironmentPopulationCallback method:

CreateEndpointReferenceEnvironmentPopulationCallback_IsAmbiguousProblem ContainsAmbiguousEndpoints_method

We only had 1 service referenced by "WithReference(...)", and the referenced service's only port was duplicated perfectly, with a naming convention exactly as of the example listed before.

DamianEdwards commented 6 months ago

@ReubenBond is this fixed now?

ReubenBond commented 6 months ago

Yes, I believe it is, since the endpoint is always a part of the config path now