简体   繁体   English

DNN / 2SXC / Razor / c# / 使用可重用的 HttpClient 使用外部 API

[英]DNN / 2SXC / Razor / c# / Consume External API with Reusable HttpClient

I am building an app in 2SXC for DNN using Razor templates.我正在使用 Razor 模板在 2SXC 中为 DNN 构建一个应用程序。 I need to pull some data from a couple external APIs, but have concerns over socket exhaustion using many HttpClient requests across different pages.我需要从几个外部 API 中提取一些数据,但担心在不同页面上使用许多 HttpClient 请求时套接字耗尽。

I have looked into using dependency injection and creating Typed Clients using IHttpClientFactory, but the current implementation in DNN doesn't appear to support this.我已经研究过使用依赖注入并使用 IHttpClientFactory 创建类型化客户端,但 DNN 中的当前实现似乎不支持这一点。

Does anyone have any suggestions on how to create and reuse HttpClient objects using razor modules that is thread safe?有没有人对如何使用线程安全的 razor 模块创建和重用 HttpClient 对象有任何建议?

Thanks for any guidance you can provide!感谢您提供的任何指导!

Just my 2c but the article you're referring to is for .net core, intended for very high-load environments.只是我的 2c,但您所指的文章适用于 .net 核心,适用于非常高负载的环境。 So I'm almost certain your DNN endpoint probably won't have this.所以我几乎可以肯定你的 DNN 端点可能不会有这个。

Either way: from further research it looks like无论哪种方式:从进一步研究来看,它看起来像

  1. The Socket-Exhaustion mentioned is your program using sockets on it's own system (so not the remote api, your sockets are being blocked for a short time)提到的 Socket-Exhaustion 是你的程序在它自己的系统上使用套接字(所以不是远程 api,你的套接字被阻塞了很短的时间)
  2. You could easily just use a singleton - which is ok for 99% of all scenarios (create a static variable. The only downside I read of is that it caches DNS, but that's rarely important if your remote endpoint is always the same server, and there are workarounds like http://byterot.blogspot.com/2016/07/singleton-httpclient-dns.html您可以轻松地使用单例 - 这适用于 99% 的所有场景(创建一个static变量。我读到的唯一缺点是它缓存了 DNS,但如果您的远程端点始终是同一台服务器,那么这并不重要,并且有像http://byterot.blogspot.com/2016/07/singleton-httpclient-dns.html这样的解决方法

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

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