Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple HttpMessageHandler to HttpClient without HttpClientFactory

I have a console application that uses HttpClient to make web requests.

var client = new HttpClient();

I'm trying to add multiple HttpMessageHandler to it (custom implementations of DelegatingHandler, really) but the constructor for HttpClient only takes a single HttpMessageHandler.

class LoggingHandler : DelegatingHandler { //... }
class ResponseContentProcessingHandler : DelegatingHandler { //... }

this is ok...

var client = new HttpClient(new LoggingHandler()); // OK

but this doesn't compile:

var client = new HttpClient(
                new LoggingHandler(), 
                new ResponseContentProcessingHandler()); // Sadness

Because I'm targeting .NET 4.0, I cannot use HttpClientFactory, which is how the solution to this problem is commonly explained:

HttpClient client = HttpClientFactory.Create(
                       new LoggingHandler(),
                       new ResponseContentProcessingHandler());

Because I'm just in a console application, rather than in an ASP.NET application, I can't do this either:

GlobalConfiguration.Configuration
                   .MessageHandlers
                   .Add(new LoggingHandler()
                   .Add(new ResponseContentProcessingHandler());

I've looked at the source for HttpClientFactory and there doesn't seem to be anything in there that wouldn't compile in .NET 4.0, but short of rolling my own factory ("inspired" by Microsoft's source code), is there a way to manually add many HTTP message handlers to the HttpClient?

like image 603
Jeff Avatar asked Mar 17 '16 16:03

Jeff


People also ask

How to add custom handler to HttpClient?

Adding Message Handlers to the Client Pipeline HttpClient client = HttpClientFactory. Create(new Handler1(), new Handler2(), new Handler3()); Message handlers are called in the order that you pass them into the Create method. Because handlers are nested, the response message travels in the other direction.

What are the differences between HttpClientFactory and HttpClient?

It has a method CreateClient which returns the HttpClient Object. But in reality, HttpClient is just a wrapper, for HttpMessageHandler. HttpClientFactory manages the lifetime of HttpMessageHandelr, which is actually a HttpClientHandler who does the real work under the hood.

What is HttpClientHandler c#?

The HttpClient class uses a message handler to process the requests on the client side. The default handler provided by the dot net framework is HttpClientHandler. This HTTP Client Message Handler sends the request over the network and also gets the response from the server.

Is AddHttpClient a singleton?

Http NuGet package that includes the AddHttpClient extension method for IServiceCollection. This extension method registers the internal DefaultHttpClientFactory class to be used as a singleton for the interface IHttpClientFactory .


1 Answers

DelegatingHandler has a protected constructor that takes a handler for the inner handler. If you have control over all your custom handlers, I would think you can add a public constructor that calls the protected constructor, like:

public class CustomHandler : DelegatingHandler
{
    public CustomHandler(HttpMessageHandler innerHandler) : base(innerHandler)
    {
    }
}

and chain them thus:

var client = new HttpClient(
    new CustomHandler(
        new OtherCustomerHandler(
            new HttpClientHandler()
        )
    )
);
like image 122
Jeff Shepler Avatar answered Sep 18 '22 22:09

Jeff Shepler