具有多个API和查询字符串的mvc.net核心Web应用中的httpclient重用 [英] httpclient reuse in mvc.net core web app with multiple apis and querystrings

查看:55
本文介绍了具有多个API和查询字符串的mvc.net核心Web应用中的httpclient重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于此问题,我读了很多矛盾的答案,但仍然没有弄清楚。
我有一个.net core 1.1网络应用程序,该应用程序可以使用许多用于各种数据源的外部api。我的当前实现使用DI将自定义的 webclient类传递给每个控制器,该控制器在内部为每个请求创建并处理一个httpclient(在using块内)。我在服务器上看到大量的空闲连接,因此我很确定这是造成这些连接的原因。我还遇到了.net核心应用崩溃和无法响应的问题,因此我想知道是否打开的连接过多导致了此问题。

I've read a lot of conflicting answers about this and still don't have it straight. I have a .net core 1.1 web app that hits a lot of external api's for various data sources. My curent implementation uses DI to pass a custom "webclient" class to each controller, which internally creates and disposes a httpclient for each request (inside a using block). I see a ton of idle connections on the server so I'm pretty sure this is causing these. I also have some issues with the .net core app crashing and no longer responding, so I'm wondering if too many connections open is creating this problem.

我意识到httpclient可以用于许多连接,但是我不清楚在某些方面。我已经读过,由于网络请求的并行性,重用同一个httpclient对于胖客户端应用程序确实更有用,而对Web应用程序则没有那么有用。我还读过您想缓存同一个httpclient以便重复使用以用于不同的URL,但是我的url使用querystrings传递参数,所以这是否意味着每个完全限定的URL都是唯一的?

I realize that httpclient can be reused for many connections, but I'm not clear on several things. I've read that reusing the same httpclient is really more useful for thick client apps and not as useful with web apps due to the very parallel nature of the web requests. I've also read that you want to cache the same httpclient for reuse for a distinct URL, but my url's use querystrings to pass parameters, so would this not mean that every fully qualitified URL is unique from the others?

我的想法是为我正在访问的api的每个基本域名缓存一个httpclient实例,但是我没有找到确认是否会给我带来好处使用单个httpclient实例。我以为我的目标是重用空闲的连接,这能达到目的吗?

My idea is to possibly cache a single instance of httpclient for each base domain name of the api's I am accessing, but I haven't found confirmation if this would give the benefits I'm looking for with using a single httpclient instance. I assume my goal is to reuse idled connections, will this accomplish this?

我还遇到了对类似于我正在执行的解决方案的引用,该解决方案将缓存这些实例,但是然后检查它们是否在某个时候被处置了,并且根据需要重新创建它们。

I also came across a reference to a solution similar to what I'm doing that would cache these instances but then check if any of them have become disposed at some point and recreate them as needed.

看起来像这样简单的东西,但是那里有很多冲突的信息!

Seems like this should be easy stuff but there is a ton of conflicting info out there!

推荐答案

HttpClient可能会在应用程序的生命周期或更长时间内保持打开状态,即使它确实实现了IDisposable。因此,使用HttpClient的单个实例可以使您走上正确的路,因为可以确保您随时都只能打开一个连接。

HttpClient can potentially stay opened during the lifetime of the application or longer, even though it does implement IDisposable. So you are on the right track with using a single instance of HttpClient, because you are guaranteed to only have one connection open at any time.

.NET Core具有帮助程序功能用于DI。如果使用以下代码,则将Webclient的单例实例注入所有使用它的类中。这意味着每个类都将使用webclient对象的相同实例,因此使用相同的httpclient。

.NET Core has helper functions for DI. If you use the following a singleton instance of your webclient will be injected into all classes which consume it. This means that every class will be using the same instance of the webclient object, therefore use the same httpclient.

在startup.cs ConfigureServices函数中使用以下内容:

Use the following in your startup.cs ConfigureServices function:

services.AddSingleton<IWebClient>(new webclient(params));    

services.AddSingleton<IWebClient, webclient>();




单生命周期服务是首次创建
请求(如果在其中指定实例
则运行ConfigureServices时),然后每个后续请求将使用相同的实例。
如果您的应用程序需要单例行为,建议让服务
容器来管理服务的生存期,而不是
实现类的设计模式并自己在类中管理对象的
生存期

Singleton lifetime services are created the first time they're requested (or when ConfigureServices is run if you specify an instance there) and then every subsequent request will use the same instance. If your application requires singleton behavior, allowing the services container to manage the service's lifetime is recommended instead of implementing the singleton design pattern and managing your object's lifetime in the class yourself.

。NET Core中DI的Microsoft文档

这篇关于具有多个API和查询字符串的mvc.net核心Web应用中的httpclient重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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