我有一个委托处理程序,它根据从服务器收到的错误消息抛出特定的异常。 这允许客户端重试(使用polly,但正如您将看到的机制无关紧要) 在两次尝试失败之后,第三次尝试只是挂起。没有数据命中服务器,也没有进一步发生。这似乎是因为HttpClient最多有两个连接,这表明这些连接没有正确关闭。 我可以做些什么来关闭连接,还是应该更改设计,以便委托处理程序不会抛出异常?
请参阅下面的简单示例以重现
private class Test : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = await base.SendAsync(request, cancellationToken);
throw new Exception("afesaf");
}
}
[HttpGet]
[Route("B")]
public async Task B()
{
var handler = new HttpClientHandler();
var pipeline = HttpClientFactory.CreatePipeline(handler, new[] { new Test() });
var http = new HttpClient(pipeline);
http.BaseAddress = new Uri("http://google.com");
var count = 0;
while (count++ < 5)
try
{
Log.Info("A");
var request = new HttpRequestMessage(HttpMethod.Get, "");
await http.SendAsync(request, HttpCompletionOption.ResponseContentRead, CancellationToken.None).ConfigureAwait(false);
}
catch
{
}
}
答案 0 :(得分:0)
处理响应对象。
我在委托处理程序中使用了以下内容,因为我有多个点可以在实际代码中抛出异常
adb shell