Skip to content

Service discovery on MapForwarder disabled when httpClient is assigned #2838

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
RamType0 opened this issue May 1, 2025 · 6 comments
Open
Labels
Type: Bug Something isn't working

Comments

@RamType0
Copy link

RamType0 commented May 1, 2025

Describe the bug

A clear and concise description of what the bug is.

To Reproduce

  1. Make Aspire project and create Yarp project with reference to api service.
  2. Setup forwarder with following code.
var transformer = app.ServiceProvider.GetRequiredService<ITransformBuilder>().Create(transformBuilderContext
    => transformBuilderContext
    .AddPathRemovePrefix("/Api"));
#if ASSIGN_HTTP_CLIENT
var httpClient = new HttpMessageInvoker(new SocketsHttpHandler
{
    UseProxy = false,
    AllowAutoRedirect = true,
    ConnectTimeout = TimeSpan.FromSeconds(30),
});
app.MapForwarder("/Api/{**apiPath}", "https://api", ForwarderRequestConfig.Empty, transformer, httpClient);

#else
app.MapForwarder("/Api/{**apiPath}", "https://api", ForwarderRequestConfig.Empty, transformer);
#endif
  1. Access to forwarder's path and it works correctly.
  2. define ASSIGN_HTTP_CLIENT.
  3. Access to forwarder's path.
  4. It respond with error codes, and logs say "No such host".

Further technical details

  • Include the version of the packages you are using
  • Yarp.Reverse Proxy 2.3.0
  • The platform (Linux/macOS/Windows)
  • Windows 11 Pro 10.0.26100 Build 26100
@RamType0 RamType0 added the Type: Bug Something isn't working label May 1, 2025
@samsp-msft
Copy link
Contributor

IIRC Aspire sets up its own HttpClientFactory that has service discovery hooked up.

@ReubenBond - If they get an instance of HttpClient from the default HttpClientFactory, can they then set properties on it before handing to YARP.

@MihaZupan
Copy link
Member

If you want to have service discovery on a custom client, you can wrap it in the same way Aspire does it for you by default: get IServiceDiscoveryHttpMessageHandlerFactory from DI and wrap your handler via CreateHandler.

@RamType0
Copy link
Author

RamType0 commented May 2, 2025

If you want to have service discovery on a custom client, you can wrap it in the same way Aspire does it for you by default: get IServiceDiscoveryHttpMessageHandlerFactory from DI and wrap your handler via CreateHandler.

It seems that good workaround.

But I feel that current behavior is inconsistent.

Whether service discovery is enabled on Yarp should not depend on whether httpClient has been assigned.

@MihaZupan
Copy link
Member

I see it as the same scenario as when you're using the regular IHttpClientFactory.
If you add handlers to the factory, your HttpClients will have modified behavior.
But if you create a new HttpClient somewhere manually without the factory, it doesn't matter how the factory was configured.
Using AddHttpForwarderWithServiceDiscovery/AddServiceDiscoveryForwarderFactory is the same idea, you're only modifying the factory.

Have you tried using AddServiceDiscoveryDestinationResolver instead? I'd expect that to work regardless of how you create the client.

@RamType0
Copy link
Author

Have you tried using AddServiceDiscoveryDestinationResolver instead? I'd expect that to work regardless of how you create the client.

How could we call AddServiceDiscoveryDestinationResolver when using IEndpointRouteBuilder.MapForwarder?

@MihaZupan
Copy link
Member

Ah yeah nvm, it wouldn't help you with MapForwarder extensions, only if you used the full config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants