简体   繁体   English

对于每个请求,RestClient 应该是 singleton 还是新的

[英]Should RestClient be singleton or new for every request

ASP.Net HttpClient is disposable, and a lot of articles say you should use the singleton pattern to use it because of the performance. ASP.Net HttpClient是一次性的,很多文章说你应该使用 singleton 模式来使用它,因为性能。 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.但是当我看到RestClient时,它无法被处理,并且在Recommended-Usage页面中,示例每次都会new RestClient Should I use singleton pattern for RestClient or should I new it every time?我应该为 RestClient 使用RestClient模式还是每次都使用new模式? If I new it every time will there be any performance concern?如果我每次都new它会有性能问题吗?

RestSharp GitHub RestSharp GitHub

Some references:一些参考:

Do HttpClient and HttpClientHandler have to be disposed 是否必须处理 HttpClient 和 HttpClientHandler

YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE 您错误地使用了 HTTPCLIENT,它正在破坏您的软件

should I use singleton pattern for RestClient or should I new it everytime, if I new it everytime will any performance concern?我应该为 RestClient 使用单例模式还是应该每次都更新它,如果我每次都更新它会不会有任何性能问题?

Recommended way to use RestSharp is to create a new instance per request.使用RestSharp推荐方法是为每个请求创建一个新实例。

It differs from Singleton approach recommended for HttpClient .它不同于为HttpClient推荐的 Singleton 方法。 And the reason is that under the hood RestSharp uses HttpWebRequest for HTTP interaction, not HttpClient .原因是RestSharp RestSharp使用HttpWebRequest进行 HTTP 交互,而不是HttpClient That's why the usage model differs.这就是使用模型不同的原因。

If I create it everytime do I get performance issue just like the HttpClient?如果我每次都创建它,我会像 HttpClient 一样遇到性能问题吗?

The main reason why you shouldn't create a new instance of HttpClient for each request is not a performance consideration.不应该为每个请求创建一个新的HttpClient实例的主要原因不是性能考虑。 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 is intended to be instantiated once and re-used throughout the life of an application. HttpClient 旨在实例化一次并在应用程序的整个生命周期中重复使用。 Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads.为每个请求实例化一个 HttpClient 类将耗尽重负载下可用的套接字数量。 This will result in SocketException errors.这将导致 SocketException 错误。

RestSharp does not use connection pool as HttpClient and does not leave opened sockets after the use. RestSharp不使用连接池作为HttpClient并且在使用后不会留下打开的套接字。 That's why it is safe (and recommended) to create a new instance of RestClient per request.这就是为什么为每个请求创建一个新的RestClient实例是安全的(并且推荐)。

Will you gain any performance improvement if you use reuse instance of RestClient ?如果您使用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.然而,这个时间非常接近于0 ,而且它是跟踪网络调用所花费的时间的一小部分。 You don't reuse other .NET objects like List<T> because of performance considerations, are you?出于性能方面的考虑,您不会重用其他 .NET 对象,例如List<T> ,是吗? You should do the same for RestClient .你应该对RestClient做同样的RestClient It's just developed in a way that implies such usage scenario.它只是以暗示这种使用场景的方式开发的。

From version v107 you should create only one instance.从版本v107 ,您应该只创建一个实例。 This version is using HttpClient internally.此版本在内部使用HttpClient

Do not instantiate RestClient for each HTTP call.不要为每个 HTTP 调用实例化 RestClient。 RestSharp creates a new instance of HttpClient internally, and you will get lots of hanging connections, and eventually exhaust the connection pool. RestSharp 在内部创建一个新的 HttpClient 实例,你会得到很多挂起的连接,并最终耗尽连接池。

If you use a dependency-injection container, register your API client as a singleton.如果您使用依赖注入容器,请将您的 API 客户端注册为 singleton。

RestClient lifecycle RestClient 生命周期

If you are wondering why this is the case, you can see explanation here如果您想知道为什么会这样,您可以在此处查看解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM