Questions tagged [httpclientfactory]

Refers to the System.Net.Http.IHttpClientFactorylibrary introduced in Asp.Net Core 2.1+ and is factory for creating HttpClient instances. It is usually associated with the [dotnet-httpclient] and [asp.net-core] tags

HttpClientFactory was created since .NET Core 2.1 to implement resilient HTTP requests by addressing issues associated the with original and well-known HttpClient

157 questions
83
votes
2 answers

Should HttpClient instances created by HttpClientFactory be disposed?

So, I've registered a named client with the services collection in my Startup.cs: services.AddHttpClient(someServiceName, client => client.BaseAddress = baseAddress); and now can inject an IHttpClientFactory from my service…
60
votes
7 answers

How to mock the new HttpClientFactory in .NET Core 2.1 using Moq

.NET Core 2.1 comes with this new factory called HttpClientFactory, but I can't figure out how to mock it to unit test some methods that include REST service calls. The factory is being injected using .NET Core IoC container, and what the method…
Mauricio Atanache
  • 2,424
  • 1
  • 13
  • 18
52
votes
2 answers

How to use HttpClientHandler with HttpClientFactory in .NET Core

I want to use the HttpClientFactory that is available in .NET Core 2.1 but I also want to use the HttpClientHandler to utilize the AutomaticDecompression property when creating HttpClients. I am struggling because the .AddHttpMessageHandler<> takes…
34
votes
2 answers

How to Refresh a token using IHttpClientFactory

I am using IHttpClientFactory for sending requests and receiving HTTP responses from two external APIs using Net Core 2.2. I am looking for a good strategy to get a new access token using a refresh token that has been stored in the appsettings.json.…
D.B
  • 4,009
  • 14
  • 46
  • 83
34
votes
4 answers

Can I use HttpClientFactory in a .NET.core app which is not ASP.NET Core?

I have read the popular blog post https://www.stevejgordon.co.uk/introduction-to-httpclientfactory-aspnetcore on using HttpClientFactory To quote from it A new HttpClientFactory feature is coming in ASP.NET Core 2.1 which helps to solve some…
Noel
  • 5,037
  • 9
  • 46
  • 69
28
votes
2 answers

Configure HttpClientFactory to use data from the current request context

With the new HttpClientFactory in ASP.NET Core 2.1, it's quite easy to configure custom HTTP clients with things like base urls, default headers etc. However, I haven't found a way to centralize configuration that lets me inject headers from the…
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
25
votes
3 answers

Create default HttpClientFactory for integration test

I have a typed client which I want to register as a singleton: public class SomeHttpClient { private readonly IHttpClientFactory _clientFactory; public SomeHttpClient(IHttpClientFactory clientFactory) { _clientFactory =…
dustinmoris
  • 3,195
  • 3
  • 25
  • 33
19
votes
3 answers

Typed HttpClient vs IHttpClientFactory

Is there any difference between the following 2 scenarios of setting up HttpClient? Should I prefer one to another? Typed client: public class CatalogService { private readonly HttpClient _httpClient; public CatalogService(HttpClient…
19
votes
1 answer

How to use ConfigurePrimaryHttpMessageHandler generic

I want to add an HttClientHandler for a Typed HttpClient in order to include certificate authentication. All the examples I'm finding on the internet are like this: services.AddHttpClient() …
19
votes
1 answer

Adding a handler to all clients created via IHttpClientFactory?

Is there a way to add a handler to all clients created by the IHttpClientFactory? I know you can do the following on named clients: services.AddHttpClient("named", c => { c.BaseAddress = new Uri("TODO"); …
Hawkzey
  • 1,088
  • 1
  • 11
  • 21
16
votes
1 answer

What is the difference between AddTransientHttpErrorPolicy and AddPolicyHandler?

I want to apply resiliency strategy using Polly. I am using HttpClientFactory from ASP.NET Core 2.1. I found some guide on Polly GitHub wiki. There are two ways of such policy configuration - using AddTransientHttpErrorPolicy and AddPolicyHandler,…
15
votes
2 answers

How to configure web proxy for HttpClient created directly via HttpClientFactory?

By directly I mean without Asp.Net Core IoC/DI helpers. I didn't find docs about it and I think my current solution isn't optimal because the handler lifecyle isn't managed by the HttpClientFactory: var proxiedHttpClientHandler = new…
15
votes
2 answers

Passing IHttpClientFactory to .NET Standard class library

There's a really cool IHttpClientFactory feature in the new ASP.NET Core 2.1 https://www.hanselman.com/blog/HttpClientFactoryForTypedHttpClientInstancesInASPNETCore21.aspx I'm trying to use this feature in my ASP.NET Core 2.1 Preview-2 app but I…
Sam
  • 26,817
  • 58
  • 206
  • 383
13
votes
6 answers

Why HttpClient does not hold the base address even when it`s set in Startup

In my .net core web api project I would like to hit an external API so that I get my response as expected. The way I`m registering and using the HttpClient is as follows. In the startup, I'm adding the following code which is called named typed…
13
votes
1 answer

How to Throttle all outgoing asynchronous calls to HttpClient across multiple threads in .net Core API project

I'm designing a .net core web api that consumes an external api that I do not control. I've found some excellent answers on stack overflow that allowed me to throttle my requests to this external API while in the same thread using semaphoreslim. …
1
2 3
10 11