HttpClientFactory.Create与新的HttpClient [英] HttpClientFactory.Create vs new HttpClient

查看:670
本文介绍了HttpClientFactory.Create与新的HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇 HttpClientFactory 。没有说明为什么它在MSDN上存在(请参阅链接)。

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

创建方法专业参数,但大多数情况下我想知道没有参数的调用和普通构造函数之间有什么区别。

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

var httpClient = HttpClientFactory.Create();

VS

var httpClient = new HttpClient();






在大多数示例中,我看到使用新的HttpClient(),不使用任何语句,即使 HttpClient 源自 IDisposable

由于 HttpClient 类从 IDisposable 派生而来,工厂是否进行了一些池化或缓存?有性能上的好处,还是没关系?

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

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

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 Core中使用 IHttpClientFactory 的信息,请参见下面的Ali Bayat的答案。

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

但是,我保留Darrel Miller的答案作为可接受的答案,因为这是在.NET Framework v4.8之前使用的正确答案,对此问题提出了要求。

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.

With。 NET 5 .NET Framework和.NET Core之间的差异将得到解决,您应该使用 IHttpClientFactory

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

推荐答案

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

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.

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

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.

我很高兴您没有看到很多示例围绕HTTPClient使用块的经验,因为我一直在与这种实践作斗争,这感觉似乎已经很多年了。尽管HttpClient确实实现了一次性使用,但它仅用于处理在请求进行期间销毁它的异常情况。 HttpClient实例应长期存在。强行处理它们会关闭应该被池化的基础TCP连接。 HttpClient是线程安全的,并且可以被不同的线程安全使用多次。这就是我经常看到的使用块模式的意图,而不是一次性使用。

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.

这篇关于HttpClientFactory.Create与新的HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆