Skip to content
This repository has been archived by the owner on Nov 17, 2018. It is now read-only.

Question: How to use HttpClientHandler with IHttpClientFactory #71

Closed
evertmulder opened this issue Mar 4, 2018 · 9 comments
Closed
Labels
Milestone

Comments

@evertmulder
Copy link

A question on how to use the IHttpClientFactory. I would like to use a named client, but with a HttpClientHandler.

This how I used to create a HttpClient

var handler = new HttpClientHandler
{
    AllowAutoRedirect = false,
    AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
...
};
_httpClient = new HttpClient(handler);

But I would like to use the IHttpClientFactory like this:

startup.cs

services.AddHttpClient("named", c =>
{
    c.BaseAddress = new Uri("TODO");
    c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    c.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
    {
        NoCache = true,
        NoStore = true,
        MaxAge = new TimeSpan(0),
        MustRevalidate = true
    };
});

How can I pass the HttpClientHandler to the named HttpClient. I was not able to find this. Any hints on how to achieve this?

@martincostello
Copy link
Contributor

Looks like you call ConfigurePrimaryHttpMessageHandler() as part of using the HttpClient builders:

public static IHttpClientBuilder ConfigurePrimaryHttpMessageHandler(this IHttpClientBuilder builder, Func<HttpMessageHandler> configureHandler)

@rynowak
Copy link
Member

rynowak commented Mar 5, 2018

Yes, @martincostello is right. You can configure the primary handler used by the factory for each named client this way.

@evertmulder
Copy link
Author

evertmulder commented Mar 5, 2018

@martincostello @rynowak Thanks a lot for pointing me in the right direction. I will test this tomorrow, but the following should do the trick.

services.AddHttpClient("named", c =>
{
    c.BaseAddress = new Uri("TODO");
    c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    c.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
    {
        NoCache = true,
        NoStore = true,
        MaxAge = new TimeSpan(0),
        MustRevalidate = true
    };
}).ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
    AllowAutoRedirect = false,
    AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
});

@rynowak
Copy link
Member

rynowak commented Mar 5, 2018

Great! glad we could help

@Timovzl
Copy link

Timovzl commented Aug 20, 2018

What if the handler must specify client certificates that are determined at runtime?

@davidfowl
Copy link
Member

What’s the complication there?

@Timovzl
Copy link

Timovzl commented Aug 22, 2018

@davidfowl To clarify, I see the HttpClientHandler being created at configuration time. At this point in the application (startup), we do not have the necessary information.

Each time we want to use the HttpClient, we load a certificate (perhaps based on a fingerprint), with the intention of getting an HttpClientHandler that uses that particular certificate. How can we achieve this?

@fleed
Copy link

fleed commented Nov 5, 2018

@Timovzl I'm facing the same problem. I was expecting an overload of the AddHttpClient method providing the IServiceProvider instance, as it is in other circumstances.
Did you find an alternative solution?

@rynowak
Copy link
Member

rynowak commented Nov 9, 2018

Please open a new issue rather than commenting on a closed issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants