对于每个请求,RestClient 应该是单例的还是新的 [英] Should RestClient be singleton or new for every request

查看:288
本文介绍了对于每个请求,RestClient 应该是单例的还是新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.Net HttpClient 是一次性的,很多文章说你应该使用单例模式来使用它,因为性能.但是当我看到 RestClient 它不能被处理,并且在 Recommended-Usage 页面示例每次都会new RestClient.我应该为 RestClient 使用单例模式,还是每次都应该 new ?如果我每次都new 会不会有性能问题?

ASP.Net HttpClient is disposable, and a lot of articles say you should use the singleton pattern to use it because of the performance. But when I see the RestClient it can't be disposed, and in the Recommended-Usage page the sample will new the RestClient every time. Should I use singleton pattern for RestClient or should I new it every time? If I new it every time will there be any performance concern?

RestSharp GitHub

一些参考:

做HttpClient 和 HttpClientHandler 必须被释放

您使用的 HTTP 客户端有误,这会破坏您的软件的稳定性

推荐答案

我应该为 RestClient 使用单例模式还是应该新建它每次,如果我每次都更新它会不会有任何性能问题?

should I use singleton pattern for RestClient or should I new it everytime, if I new it everytime will any performance concern?

推荐使用 RestSharp 的方法是为每个请求创建一个新实例.

Recommended way to use RestSharp is to create a new instance per request.

它不同于为 HttpClient 推荐的单例方法.原因是幕后 RestSharp 使用 HttpWebRequest 进行 HTTP 交互,而不是 HttpClient.这就是使用模型不同的原因.

It differs from Singleton approach recommended for HttpClient. And the reason is that under the hood RestSharp uses HttpWebRequest for HTTP interaction, not HttpClient. That's why the usage model differs.

如果我每次都创建它,我是否会遇到性能问题,就像HttpClient?

If I create it everytime do I get performance issue just like the HttpClient?

不应该为每个请求创建 HttpClient 的新实例的主要原因不是性能考虑.用于创建和初始化所花费的时间将占用用于后续网络调用的一小部分时间.使用 HttpClient 单例实例的主要原因 如下:

The main reason why you shouldn't create a new instance of HttpClient for each request is not a performance consideration. The time spent for creation and initialization will take a tiny fraction of time spent for following network call. The main reason to use singleton instance of HttpClient is the following:

HttpClient 旨在实例化一次并在整个过程中重复使用应用程序的生命周期.实例化一个 HttpClient 类每个请求都会耗尽重载下可用的套接字数量负载.这将导致 SocketException 错误.

HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors.

RestSharp 不使用连接池作为 HttpClient 并且在使用后不会留下打开的套接字.这就是为什么为每个请求创建一个新的 RestClient 实例是安全的(并且推荐).

RestSharp does not use connection pool as HttpClient and does not leave opened sockets after the use. That's why it is safe (and recommended) to create a new instance of RestClient per request.

如果您使用 RestClient 的重用实例,您会获得任何性能改进吗?那么,您将节省创建对象及其初始化的时间.然而,这个时间非常接近 0,而且它是用于跟踪网络调用的一小部分时间.出于性能方面的考虑,您不会重用其他 .NET 对象,例如 List,是吗?你应该对 RestClient 做同样的事情.它只是以暗示这种使用场景的方式开发.

Will you gain any performance improvement if you use reuse instance of RestClient? Well, you will save the time for creation of object and its initialization. However this time is very close to 0 and moreover it's a tiny fraction of time spent for following network call. You don't reuse other .NET objects like List<T> because of performance considerations, are you? You should do the same for RestClient. It's just developed in a way that implies such usage scenario.

这篇关于对于每个请求,RestClient 应该是单例的还是新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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