Question: How to use HttpClientHandler with IHttpClientFactory #71
Comments
Looks like you call HttpClientFactory/src/Microsoft.Extensions.Http/DependencyInjection/HttpClientBuilderExtensions.cs Line 140 in 4d976f6
|
Yes, @martincostello is right. You can configure the primary handler used by the factory for each named client this way. |
@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
}); |
Great! glad we could help |
What if the handler must specify client certificates that are determined at runtime? |
What’s the complication there? |
@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? |
@Timovzl I'm facing the same problem. I was expecting an overload of the |
Please open a new issue rather than commenting on a closed issue. |
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
But I would like to use the IHttpClientFactory like this:
startup.cs
How can I pass the HttpClientHandler to the named HttpClient. I was not able to find this. Any hints on how to achieve this?
The text was updated successfully, but these errors were encountered: