Using Steeltoe DiscoveryHttpMessageHandler with FlurlClient

I am looking to switch our HttpClients to use Flurl. However, our HttpClient is currently configured to use Service Discovery via Steeltoe. Basically it's doing this in ConfigureServices:

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

DiscoveryHttpMessageHandler is a custom http message handler in the Steeltoe library (https://github.com/SteeltoeOSS)

How do I access the IHttpClientBuilder with Flurl so I can add this same message hander? Or is there another clean way with Flurl to add a custom message handler for every HttpClient/FlurlClient created?

1 answer

  • answered 2021-03-09 14:22 Todd Menier

    There's a few ways to add a custom message handler with Flurl (such as with a custom factory), but since you're already using IHttpClientFactory, I think the easiest path to get what you want (and the one I'd recommend) is to continue injecting HttpClient into your services as you're doing and wrap them with Flurl inside the service:

    public class MyClass : IMyClass
    {
        private readonly IFlurlClient _flurlClient;
    
        public MyService(HttpClient httpClient) {
            _flurlClient = new FlurlClient(httpClient);
        }
    }