将 Steeltoe DiscoveryHttpMessageHandler 与 FlurlClient 结合使用

Using Steeltoe DiscoveryHttpMessageHandler with FlurlClient

我希望将我们的 HttpClient 切换为使用 Flurl。但是,我们的 HttpClient 当前配置为通过 Steeltoe 使用服务发现。基本上它在 ConfigureServices 中这样做:

   services.AddHttpClient<IMyClass, MyClass>().AddHttpMessageHandler<DiscoveryHttpMessageHandler>();

DiscoveryHttpMessageHandler 是 Steeltoe 库中的自定义 http 消息处理程序 (https://github.com/SteeltoeOSS)

如何使用 Flurl 访问 IHttpClientBuilder 以便添加相同的消息处理程序?或者是否有另一种使用 Flurl 的简洁方法为每个创建的 HttpClient/FlurlClient 添加自定义消息处理程序?

有几种方法可以使用 Flurl 添加自定义消息处理程序(例如使用自定义 factory),但由于您已经在使用 IHttpClientFactory,我认为最简单的方法你想要的(也是我推荐的)是继续将 HttpClient 注入你的服务,并在服务中用 Flurl 包装它们:

public class MyClass : IMyClass
{
    private readonly IFlurlClient _flurlClient;

    public MyService(HttpClient httpClient) {
        _flurlClient = new FlurlClient(httpClient);
    }
}