Asp.net core Service Fabric Asp.net Core Kestrel HttpClient以最小负载挂起
我有一个barebone Service Fabric应用程序,它托管一个Asp.net Core 1.1 Web API,Azure Application Gateway作为反向代理,位于一个5 DS3_V2的虚拟机规模集上 该API有10个HttpClient,它们通过依赖项注入注入了不同的URL。 方法调用中的一个简单foreach循环并行调用10个HttpClient:Asp.net core Service Fabric Asp.net Core Kestrel HttpClient以最小负载挂起,asp.net-core,asp.net-core-mvc,azure-service-fabric,asp.net-core-webapi,kestrel-http-server,Asp.net Core,Asp.net Core Mvc,Azure Service Fabric,Asp.net Core Webapi,Kestrel Http Server,我有一个barebone Service Fabric应用程序,它托管一个Asp.net Core 1.1 Web API,Azure Application Gateway作为反向代理,位于一个5 DS3_V2的虚拟机规模集上 该API有10个HttpClient,它们通过依赖项注入注入了不同的URL。 方法调用中的一个简单foreach循环并行调用10个HttpClient: var cts = new CancellationTokenSource(); c
var cts = new CancellationTokenSource();
cts.CancelAfter(600);
//Logic for asyncronously parallel calling the Call method below
public async Task<MyResponse> Call(CancellationTokenSource cts, HttpClient client, string endpoint )
{
var endpoint = "finalpartOfThendpoint";
var jsonRequest = "jsonrequest";
try
{
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
await content.LoadIntoBufferAsync();
if (cts.Token.IsCancellationRequested)
{
return new MyResponse("Token Canceled");
}
var response = await client.PostAsync(endpoint, content, cts.Token);
if (response.IsSuccessStatusCode && ((int)response.StatusCode != 204))
{
//do something with response and return
return MyResponse("Response Ok")
}
return MyResponse("No response")
}
catch (OperationCanceledException e)
{
return new MyResponse("Timeout");
}
}
所有HttpClient都是重用的和静态的。
同样的代码在托管在OWIN in in Service Fabric上的Asp.net Web API 2上也能完美地工作。问题只存在于Asp.net Core 1.1中
我在网上看到了创建HttpClientHandler的方法,但是没有用于并发连接的参数
我可以做些什么来进一步调查?
不会引发异常,但OperationcanceledException和如果我删除CancellationToken,调用将被卡住,CPU将达到100%,基本上30个连接会破坏5个四核服务器的电源
这与红隼发出的呼叫数有关
更新 我尝试使用WebListener,但问题仍然存在,因此不是Kestrel,而是Asp.net Core,我找到了答案 与旧的Asp.net WebAPI一样,Asp.net core对于连接到同一服务器仍然有一些HttpClient限制。 它的文档很差,但maxconnections的旧ServicepointManager选项现在必须通过HttpClientHandler传递。 我只是这样创建了HttpClient,问题就消失了
var config = new HttpClientHandler()
{
MaxConnectionsPerServer = int.MaxValue
};
var client = new HttpClient(config)
{
BaseAddress = new Uri('url here')
};
确实,如果团队中有人正在阅读,这应该是默认值。您的所有HttpClient都使用相同的端点,还是为每个客户端创建端点?
var config = new HttpClientHandler()
{
MaxConnectionsPerServer = int.MaxValue
};
var client = new HttpClient(config)
{
BaseAddress = new Uri('url here')
};