20

In .Net core, we can use IHttpClientFactory to inject and use during runtime. As a developer I no need to worry about the dependency resolution. I need to just specify AddHttpClient() in service collection. Likewise, how can I use IHttpClientFactory in WPF application, .net framework 4.6.1? As there is no Service collection available for WPF application, not getting how to resolve the dependency.

Tushar Mali
  • 283
  • 1
  • 4
  • 10

1 Answers1

35

You can add the Microsoft.Extensions.Http NuGet package to any .NET Standard 2.0-compliant project. That includes projects that target .NET 4.6.1

All of the Microsoft.Extensions.* packages are .NET Standard 2.0 packages, which means you can use the same Configuration, Dependency Injection, Logging services as .NET Core.

Microsoft.Extensions.Http provides the HttpClientFactory only, not the new optimized HttpClient. This is only available in .NET Core 2.1

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 3
    The article explaining `IHttpClientFactory` says the problem is that the default `HttpClient` in **.NET Core** (with no mention made of .NET Framework) will cause socket-exhaustion issues - does the default `HttpClient` in .NET Framework 4.8 have this issue or not? If it doesn't, then there's no need to use `IHttpClientFactory` in .NET Framework projects and they can use a long-life'd singleton `HttpClient` instance, no? – Dai Jul 30 '20 at 01:41
  • 2
    @Dai I think the issue exists in both implementations. Both the [4.8](https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev16.query%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(System.Net.Http.HttpClient)%3Bk(SolutionItemsProject)%3Bk(SolutionItemsProject)%3Bk(TargetFrameworkMoniker-.NETFramework%2CVersion%3Dv4.8)%3Bk(DevLang-csharp)%26rd%3Dtrue%26f%3D255%26MSPPError%3D-2147217396&view=netframework-4.8#remarks) and 3.1 docs mention the issue. Can't link both URLs are too long. Without doing a test it is safer to use the factory. – cskwrd Aug 20 '20 at 15:23
  • 3
    @Dai no - in fact the issue is the opposite of that. Socket exhaustion is [caused when you *don't* reuse an instance](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/). The OS keeps sockets for up to 240s after you dispose an HttpClient waiting for any remaining remote packets. When you reuse an instance, you reuse the *socket* to a specific *machine* which means [any DNS changes will be missed](https://josef.codes/you-are-probably-still-using-httpclient-wrong-and-it-is-destabilizing-your-software/) – Panagiotis Kanavos Aug 20 '20 at 15:37
  • According to [microsoft httpclient guidelines](https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/httpclient-guidelines), in **Recommended use** section, it says in .NET Framework, you can create a new HttpClient each time you need to send a request. – Topman Jun 15 '22 at 00:10
  • 3
    @Topman it says "If you create a new client instance for each request, you can exhaust available sockets." You do _not_ want to create a new HttpClient for each request, because you'll exhaust sockets. – zmarks22 Sep 21 '22 at 14:59
  • @Topman you can and *should* reuse HttpClient instances. It's not just the socket exhaustion issue. By reusing HttpClient you don't have to wait for DNS resolution on every request. – Panagiotis Kanavos Sep 21 '22 at 15:07
  • I would like to add that the recommended approach: Long Lived requests should use a "static or singleton" POOLED HttpClient. For Short Lived requests use IHttpClientfactory.. The POOLING implementation is different in dotnet framework / dotnet core 5+. In dotnet framework use the ServicePointManager in dotnet core/5+ use PooledConnectionLifetime. https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines – ReidK Jan 17 '23 at 18:47
  • @ReidK that's not what the linked document says. It doesn't matter how long a request will take. Recycling won't interrupt active requests. Recycling is used to handle DNS changes which affects long and short requests equally. – Panagiotis Kanavos Jan 18 '23 at 07:53
  • @PanagiotisKanavos - what does it mean exactly when you say "Microsoft.Extensions.Http provides the HttpClientFactory only, not the new optimized HttpClient. This is only available in .NET Core 2.1"? – Jatin Jul 26 '23 at 10:48