.net HttpClientFactory.Create 与新的 HttpClient

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18976042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:27:50  来源:igfitidea点击:

HttpClientFactory.Create vs new HttpClient

.netdotnet-httpclient.net-4.6

提问by Bart Verkoeijen

I am curious what is the purpose of the HttpClientFactoryclass. There is no description of why it exists on MSDN (see link).

我很好奇HttpClientFactory上课的目的是什么。MSDN 上没有说明它存在的原因(见链接)。

There are Createmethodswith more specialized arguments, but mostly I wonder what is the difference between the call with no parameters and the normal constructor.

有一些具有更专业参数的Create方法,但我主要想知道没有参数的调用和普通构造函数之间有什么区别。



var httpClient = HttpClientFactory.Create();

VS

VS

var httpClient = new HttpClient();


In most examples I see the use of new HttpClient(), without any usingstatements, even though the HttpClientclassderives from IDisposable.

在大多数示例中,我看到使用new HttpClient(), 没有任何using语句,即使HttpClient派生自IDisposable.

Since the HttpClientclass derives from IDisposable, is there some pooling or caching done by the factory? Are there performance benefits, or does it not matter?

由于HttpClient该类派生自IDisposable,工厂是否进行了一些池化或缓存?是否有性能优势,或者无关紧要?

Update –?IHttpClientFactoryin .NET Core 2.1

更新 -?IHttpClientFactory在 .NET Core 2.1 中

Please note that since this question was asked, newer versions of .NET have been released, and .NET Core 2.1 introduced a new and much improved approach for getting a HTTP client.

请注意,自从提出这个问题以来,已经发布了较新版本的 .NET,并且 .NET Core 2.1 引入了一种新的、改进很多的方法来获取 HTTP 客户端。

Refer to Ali Bayat's answer below for using IHttpClientFactoryinstead with .NET Core.

请参阅下面的 Ali Bayat 回答以IHttpClientFactory代替 .NET Core 使用。

However, I'm keeping Darrel Miller's answer as the accepted answer since this is the correct answer for usage in .NET Framework up to v4.8, for which this question was asked.

但是,我将 Darrel Miller 的答案保留为已接受的答案,因为这是在 v4.8 之前的 .NET Framework 中使用的正确答案,为此问题提出了这个问题。

With .NET 5 the discrepancy between .NET Framework and .NET Core will be aligned, and you should use IHttpClientFactoryinstead.

在 .NET 5 中,.NET Framework 和 .NET Core 之间的差异将被对齐,您应该IHttpClientFactory改用。

回答by Darrel Miller

The factory is helper method to assist in the creation of a client when you have more than one DelegatingHandler in the pipeine. Delegating handlers need to be connected together to form a pipeline. This factory allows you to pass the handlers in as an array and the factory will take care of connecting them together.

当管道中有多个 DelegatingHandler 时,工厂是帮助创建客户端的辅助方法。委托处理程序需要连接在一起以形成管道。该工厂允许您将处理程序作为数组传入,工厂将负责将它们连接在一起。

I believe, and don't take my word for it, that the CreatePipeline method may be used over on the server side to build the message handling pipeline for a Web API HttpServer.

我相信并且不要相信我的话,可以在服务器端使用 CreatePipeline 方法来构建 Web API HttpServer 的消息处理管道。

I'm happy you are not seeing many examples of using blocks around HTTPClient as I have been fighting against this practice for what feels like years. Although HttpClient does implement disposable it only does it to handle exceptions scenarios where it gets destroyed while a request is ongoing. HttpClient instances should be long lived. Disposing them forcibly closes the underlying TCP connection that is supposed to be pooled. HttpClient is thread safe and can be safely used many times by different threads. That's how it is intended to be used, not the single use, using block pattern that I see regularly.

我很高兴你没有看到很多在 HTTPClient 周围使用块的例子,因为我多年来一直在反对这种做法。尽管 HttpClient 确实实现了一次性的,但它仅用于处理在请求正在进行时它被销毁的异常情况。HttpClient 实例应该是长期存在的。强制处理它们会关闭应该被池化的底层 TCP 连接。HttpClient 是线程安全的,可以被不同的线程安全地多次使用。这就是它的用途,而不是一次性使用,使用我经常看到的块模式。

回答by Ali Bayat

IHttpClientFactoryoffers the following benefits:

IHttpClientFactory提供以下好处:

  1. Naming and configuring logical HttpClientinstances.
  2. Build an outgoing request middlewareto manage cross-cutting concernsaround HTTP requests.
  3. Integrates with Pollyfor transient fault handling.
  4. Avoid common DNSproblems by managing HttpClientlifetimes.
  5. Adds loggingfor all requests sent through clients created by the factory.
  1. 命名和配置逻辑HttpClient实例。
  2. 构建一个传出请求中间件来管理 围绕 HTTP 请求的横切关注点
  3. Polly集成以进行瞬态故障处理。
  4. 通过管理生命周期避免常见的DNS问题HttpClient
  5. 为通过工厂创建的客户端发送的所有请求添加日志记录

more

更多的

回答by Deepak Mishra

As mentioned in

如中所述

https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#issues-with-the-original-httpclient-class-available-in-net-core

https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#issues-with-the-original-httpclient-网络核心可用类

The

default constructor for HttpClient

默认构造函数 HttpClient

has sockets exhaustion and DNS changes issues, which are addressed by IHttpClientFactory. It also provide extensions for adding resiliency to the application.

有套接字耗尽和 DNS 更改问题,这些问题由IHttpClientFactory. 它还提供扩展以增加应用程序的弹性。

回答by Dave Black

Let me add to @DarrelMiller's answer:

让我补充@DarrelMiller 的回答:

You should pay attention to the lifetime of your HttpClient instances if scaling is of any importance to you. Please refer to What is the overhead of creating a new HttpClient per call in a WebAPI client?

如果缩放对您来说很重要,您应该注意 HttpClient 实例的生命周期。请参阅在 WebAPI 客户端中每次调用创建新 HttpClient 的开销是多少?