AspNet Core 3 HttpClientFactory:TaskCanceledException
AspNet Core 3 HttpClientFactory: TaskCanceledException
我在 Azure Kubernetes 服务中使用了一个 aspnet core 3 反向代理,大致基于 ProxyKit,它在以前的集群中工作得很好。在新集群上(我能看到的唯一区别是旧集群使用 kubenet,新集群使用 azure 虚拟网络)当上游请求花费超过几秒钟。
这是抛出异常的相关方法:
return await _httpClient.SendAsync(
UpstreamRequest,
HttpCompletionOption.ResponseContentRead,
HttpContext.RequestAborted)
.ConfigureAwait(false);
HttpClient
由 HttpClientFactory
使用类型化的 AddHttpClient
中间件提供。
到目前为止我尝试过的事情:
- 为
HttpClient
明确设置 30 秒超时
- 没有将
CancelationToken
传递给 SendAsync
方法
- 按照 this article
中的建议实施自定义超时处理
HttpClientFactory
之前是这样配置的:
var httpClientBuilder = services
.AddHttpClient<ProxyKitClient>()
.ConfigurePrimaryHttpMessageHandler(sp => new HttpClientHandler
{
AllowAutoRedirect = false,
UseCookies = false
});
这是现在的配置:
var httpClientBuilder = services
.AddHttpClient<ProxyKitClient>(o => o.Timeout = Timeout.InfiniteTimeSpan)
.ConfigurePrimaryHttpMessageHandler(sp => new TimeoutHandler
{
InnerHandler = new HttpClientHandler
{
AllowAutoRedirect = false,
UseCookies = false
}
});
行为没有任何改变。
如何确保 HttpClient
等待上游请求完成? Kestrel 和 HttpClient 默认超时比现在中止的请求高得多。
附带说明一下,当我恢复到 aspnet core 2.2 时,行为完全相同。
我评论了:
Task cancellation for something like SendAsync is going to occur when the client closes the connection. As such, I think you're looking the wrong place. You need to figure out why the clients are closing the connection prematurely.
因此,OP 能够确定此问题与 Azure 应用程序网关有关:
Makes sense, and you are totally right. Was pulling my remaining hair out the whole day and the culprit is Azure Application Gateway with a request timeout of 1 second.
我在 Azure Kubernetes 服务中使用了一个 aspnet core 3 反向代理,大致基于 ProxyKit,它在以前的集群中工作得很好。在新集群上(我能看到的唯一区别是旧集群使用 kubenet,新集群使用 azure 虚拟网络)当上游请求花费超过几秒钟。
这是抛出异常的相关方法:
return await _httpClient.SendAsync(
UpstreamRequest,
HttpCompletionOption.ResponseContentRead,
HttpContext.RequestAborted)
.ConfigureAwait(false);
HttpClient
由 HttpClientFactory
使用类型化的 AddHttpClient
中间件提供。
到目前为止我尝试过的事情:
- 为
HttpClient
明确设置 30 秒超时
- 没有将
CancelationToken
传递给SendAsync
方法 - 按照 this article 中的建议实施自定义超时处理
HttpClientFactory
之前是这样配置的:
var httpClientBuilder = services
.AddHttpClient<ProxyKitClient>()
.ConfigurePrimaryHttpMessageHandler(sp => new HttpClientHandler
{
AllowAutoRedirect = false,
UseCookies = false
});
这是现在的配置:
var httpClientBuilder = services
.AddHttpClient<ProxyKitClient>(o => o.Timeout = Timeout.InfiniteTimeSpan)
.ConfigurePrimaryHttpMessageHandler(sp => new TimeoutHandler
{
InnerHandler = new HttpClientHandler
{
AllowAutoRedirect = false,
UseCookies = false
}
});
行为没有任何改变。
如何确保 HttpClient
等待上游请求完成? Kestrel 和 HttpClient 默认超时比现在中止的请求高得多。
附带说明一下,当我恢复到 aspnet core 2.2 时,行为完全相同。
我评论了:
Task cancellation for something like SendAsync is going to occur when the client closes the connection. As such, I think you're looking the wrong place. You need to figure out why the clients are closing the connection prematurely.
因此,OP 能够确定此问题与 Azure 应用程序网关有关:
Makes sense, and you are totally right. Was pulling my remaining hair out the whole day and the culprit is Azure Application Gateway with a request timeout of 1 second.