通过 Polly 重试策略识别最终重试的完成

Identify completion of final retry by Polly retry policy

如何在Polly中识别最终重试的完成(不是onRetry事件,而是实际执行完成)?

我可以将重试次数与 onRetry 事件中的最大重试次数进行比较,但该事件只是重试的开始,尚未在等待时间内发生。我想做的是确定最终重试的结束是成功还是失败。

What I want to do is identify the end of the final retry be it a success or fail.

您可以使用 Polly 的 ExecuteAndCaptureAsync() 代替 .ExecuteAsync()

ExecuteAndCaptureAsync() returns 具有 PolicyResult.Outcome == OutcomeType.SuccessfulOutcomeType.FailurePolicyResult 对象(以及文档中涵盖的其他属性)。


如果坚持使用 .ExecuteAsync() 而不是 ExecuteAndCaptureAsync(),则通过继续执行下一条语句来确定最终尝试的结束(成功时);或(失败时)根据策略 rethrowing the final exceptionExecuteAndCaptureAsync() 只是这些行为的包装。