背景:
我目前正在Azure中托管具有以下规范的ASP.NET应用程序:
我在该服务上运行了一些后台作业,该作业对第三方服务进行了很多出站HTTP调用。
问题:
在较小的负载(每10秒大约1个呼叫)下,所有请求都将在不到一秒钟的时间内完成,而不会出现问题。我遇到的问题是,在负载沉重的情况下,当服务可以在10秒的时间内完成3/4个呼叫时,某些请求将随机超时并引发异常。当我使用RestSharp时,异常将显示为“操作已超时”。现在,我正在使用Flurl,该异常显示为“呼叫超时”。
这是关键-如果我从运行Windows 10 / Visual Studios 2017的笔记本电脑运行同一作业,则不会发生此问题。这使我相信我的托管环境正在达到某个极限或用尽了一些资源。不清楚是否与连接/套接字或线程相关。
我尝试过的事情:
async/await
来防止锁定ServicePointManager
设置这是我当前用来尝试防止出现此问题的startup.cs中的代码:
public class Startup
{
public Startup(IHostingEnvironment hostingEnvironment)
{
...
// ServicePointManager setup
ServicePointManager.UseNagleAlgorithm = false;
ServicePointManager.Expect100Continue = false;
ServicePointManager.DefaultConnectionLimit = int.MaxValue;
ServicePointManager.EnableDnsRoundRobin = true;
ServicePointManager.ReusePort = true;
// Set Service point timeouts
var sp = ServicePointManager.FindServicePoint(new Uri("https://placeholder.thirdparty.com"));
sp.ConnectionLeaseTimeout = 15 * 1000; // 15 seconds
FlurlHttp.ConfigureClient("https://placeholder.thirdparty.com", cli => cli.Settings.ConnectionLeaseTimeout = new TimeSpan(0, 0, 15));
}
}
还有其他人遇到类似的问题吗?我愿意就如何最好地调试这种情况或纠正问题的可能方法提出任何建议。经过几天的研究,我完全不知所措。
谢谢。
答案 0 :(得分:3)
我有类似的问题。看看Asp.net Core HttpClient has many TIME_WAIT or CLOSE_WAIT connections。通过netstat
进行调试有助于为我确定问题。作为一种可能的解决方案。我建议您使用IHttpClientFactory
。您可以从https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2获取更多信息,如Flurl client lifetime in ASP.Net Core 2.1 and IHttpClientFactory