我正在使用 Polly 框架进行瞬态故障处理。对于同步操作,Polly 断路器策略工作正常,但是当我创建它的异步版本时,它不会重试执行。请建议:
异步方法:
private async static Task HelloWorld()
{
if (DateTime.Now < programStartTime.AddSeconds(10))
{
Console.WriteLine("Task Failed.");
throw new TimeoutException();
}
await Task.Delay(TimeSpan.FromSeconds(1));
Console.WriteLine("Task Completed.");
}
Polly 断路器异步策略:
private static void AsyncDemo3(Func<Task> action)
{
programStartTime = DateTime.Now;
Policy policy = Policy
.Handle<TimeoutException>()
.CircuitBreakerAsync(3, TimeSpan.FromSeconds(2));
try
{
var a = policy.ExecuteAndCaptureAsync(action, true).GetAwaiter().GetResult();
}
catch (AggregateException ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
执行 Polly 断路器策略:
AsyncDemo3(HelloWorld);
请帮助查找并解决问题。