【发布时间】:2020-04-29 05:03:33
【问题描述】:
我有以下代码
try
{
using (var client = HttpClientBuilder.CreateClient(PingTimeoutMilliseconds, true))
{
var response = await Policy
.Handle<Exception>(ex => ex is HttpRequestException || ex is TaskCanceledException)
.WaitAndRetryAsync(new[]
{
TimeSpan.FromSeconds(1),
TimeSpan.FromSeconds(3)
}, (ex, timeSpan, retryCount, context) =>
{
loggerService.Log(ex);
loggerService.Log($"Ping request failed. Waiting {timeSpan} seconds before next retry. Retry attempt {retryCount}");
})
.ExecuteAsync(() => client.SendAsync(new HttpRequestMessage(HttpMethod.Head, uri)));
IsInternetAvailable = response.IsSuccessStatusCode;
return response.IsSuccessStatusCode;
}
}
有些客户抱怨异常
Uitzonderingsinformatie: System.IO.FileNotFoundException
System.IO.FileNotFoundException
bij Showpad.OutlookDesktopAddIn.Infrastructure.Helpers.PingHelper+<IsInternetAvailableAsync>d__5.MoveNext()
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Start[[Showpad.OutlookDesktopAddIn.Infrastructure.Helpers.PingHelper+<IsInternetAvailableAsync>d__5, Showpad.OutlookDesktopAddIn, Version=2.7.6.0, Culture=neutral, PublicKeyToken=null]](<IsInternetAvailableAsync>d__5 ByRef)
我可能怀疑 Polly 库在第 5 行等待策略可能有问题(因为存在 IO 异常)
using Polly;
【问题讨论】:
-
Polly 在加载其他模块时没有运行时依赖,并且 Polly 本身没有 I/O(只能用于保护您通过策略执行的 I/O),所以我不会立即怀疑 Polly 有问题。
标签: c# asp.net .net asp.net-mvc polly