Skip to content

Context-Based Endpoint Resolution does not seem to work correctly when using "WithEnvironment" #8574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
b-enigma-con opened this issue Apr 6, 2025 · 8 comments
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

Comments

@b-enigma-con
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

According to article Configuring, Referencing, and Resolving Endpoints in .NET, endpoints should be resolved based on their context. When spinning up 2 containers and using "WithEnvironment", as decribed in mentioned document, the host name is always resolved as "localhost", instead of the target container hostname.This issue was encountered when following up on advice given in this issue: issue #8561

Expected Behavior

I would expect the referenced containers hostname to be resolved instead of the value "localhost".

Steps To Reproduce

The code below is all that's needed to reproduce the issue. It makes no sense for RabbitMQ to refer to Redis, but in the end these are "just" two containers refering to each other.

var builder = DistributedApplication.CreateBuilder(args);

var redis = builder.AddRedis("redis");

builder.AddRabbitMQ("rabbitmq")
    .WithEnvironment(context =>
    {
        var endpoint = redis.GetEndpoint("tcp");

        // Use .Property to access Host and Port expressions and defer evaluation
        var redisHost = endpoint.Property(EndpointProperty.Host);
        var redisPort = endpoint.Property(EndpointProperty.Port);

        // Set these values as REDIS_HOST/PORT on the target resource
        context.EnvironmentVariables["REDIS_HOST"] = redisHost;
        context.EnvironmentVariables["REDIS_PORT"] = redisPort;
    });


builder.Build().Run();

Image

Exceptions (if any)

No response

.NET Version info

9.0.201

Anything else?

No response

@github-actions github-actions bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Apr 6, 2025
@davidfowl
Copy link
Member

Could be a regression.

cc @adamint

@afscrome
Copy link
Contributor

afscrome commented Apr 6, 2025

Looks like EndpointProperty.Url is correctly resolving, but EndpointProperty.Host / EndpointProperty.Ipv4Host are not.

Image

var redis = builder.AddRedis("redis");
var endpoint = redis.GetEndpoint("tcp");

builder.AddRabbitMQ("rabbitmq")
    .WithEnvironment("REDIS_HOST", $"{endpoint.Property(EndpointProperty.Host)}")
    .WithEnvironment("REDIS_IPV4HOST", $"{endpoint.Property(EndpointProperty.IPV4Host)}")
    .WithEnvironment("REDIS_URL", $"{endpoint.Property(EndpointProperty.Url)}")
    ;

Reproed on both 9.1.0, and 9.2.0-preview.1.25204.9

  <Sdk Name="Aspire.AppHost.Sdk" Version="9.1.0" />
  <ItemGroup>
    <PackageReference Include="Aspire.Hosting.AppHost" Version="9.1.0" />
    <PackageReference Include="Aspire.Hosting.Azure.Storage" Version="9.1.0" />
    <PackageReference Include="Aspire.Hosting.Redis" Version="9.1.0" />
    <PackageReference Include="Aspire.Hosting.RabbitMq" Version="9.1.0" />
  </ItemGroup>

@davidfowl
Copy link
Member

Excellent that it's not a regression, just a bug.

@davidfowl
Copy link
Member

Logic will be here:

internal class ExpressionResolver(string containerHostName, CancellationToken cancellationToken, bool sourceIsContainer)

@davidfowl
Copy link
Member

Oh I think I recall why we did this. We don't blindly replace the host and port individually. We need to see both the host and the port to replace them.

@afscrome
Copy link
Contributor

afscrome commented Apr 6, 2025

Do you recall why that is? With EndpointProperty.Port and EndpointProperty.TargetPort, I can sort of see why we didn't remap them, but with Host, we don't expose a way to get the resolve host.

This is also getting even more confusing in 9.2 with the addition of EndpointProperty.HostAndPort as it's very counter intuitive that EndpointProperty.HostAndPort gives you something completely different to concatenating Host and Port separately.

builder.AddRabbitMQ("rabbitmq")
    .WithEnvironment("REDIS_HOST", $"{endpoint.Property(EndpointProperty.Host)}")
    .WithEnvironment("REDIS_PORT", $"{endpoint.Property(EndpointProperty.Port)}")
    .WithEnvironment("REDIS_HOSTANDPORT", $"{endpoint.Property(EndpointProperty.HostAndPort)}")
    ;

Image

@afscrome
Copy link
Contributor

afscrome commented Apr 6, 2025

I guess it's related not fully supporting #7153 . When resolving an endpoint to yourself, you usually (but not always) want the Port to reference your internal port, not your external one. That however is likely not the case if referencing the host/port of an endpoint for another resource (which is happening in this case).

@davidfowl
Copy link
Member

We don’t want to blindly replace the port as you might actually want the host port and not the target port. Generally this is an areas of ambiguity and we may just need to design the concept of “which network” you are resolving for with a way to override.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication
Projects
None yet
Development

No branches or pull requests

3 participants