microsoft / microsoft/aspire

YARP integration: unable to proxy to an external service running on localhost

Open
#10,551 3 comments 1 reaction 0 assignees View on GitHub
area-app-model
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

If a developer wants to use an existing endpoint serving from "localhost" address with YARP. it is unclear how to make it happen. I tried two ways and in both cases YARP tried to find the endpoint on its inner container network, which obviously failed.

### Expected Behavior

_No response_

### Steps To Reproduce

1. Create Aspire starter app
2. Start a "hello world" type of HTTP service listening on localhost address, port 8080 (change port as necessary).
3. Add the following to the app code

```csharp
var hellow = builder.AddExternalService("hellow", "http://localhost:8080");

builder.AddYarp("yarp")
.WithExternalHttpEndpoints()
.WithConfiguration(yc =>
{
var c = yc.AddCluster(hellow); // Also tried using hellow directly, which also compiles and runs
yc.AddRoute("/hellow/{**catchall}", c).WithTransformPathRemovePrefix("/hellow");

});
```

4. Start the app and try to hit the YARP endpoint path `/hellow`. YARP returns 502 Bad Gateway. In the error logs you will find that it tires to connect to "localhost:8080" (on the CONTAINER loopback device, not on host) and does not find anything listening on that address/port combination.

### Exceptions (if any)

_No response_

### .NET Version info

_No response_

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the Aspire starter app reproduction using AddExternalService("hellow", "http://localhost:8080") and the AddYarp configuration shown in the issue. Run the service and inspect YARP's 502 error logs to trace how localhost is resolved; done means the /hellow route successfully proxies to the external host service rather than the container loopback.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.