鉴于此代码尝试调用AuthenticationManager.Authenticate()
10 次,然后在 10 次CustomException
尝试失败后抛出:
string res = Policy
.Handle<Exception>() // Handles any exception
.OrResult<string>(result => string.IsNullOrWhiteSpace(result)) // or if result is null
.Fallback(() => throw new CustomException("Exception!")) // Not being thrown after 10 unsuccessful attempts
.Wrap(
Policy
.Handle<Exception>()
.OrResult<string>(result => string.IsNullOrWhiteSpace(result))
.WaitAndRetry(
10,
retryAttempt => TimeSpan.FromSeconds(60),
onRetry: (response, delay, retryCount, context) => Trace.WriteLine($"[{DateTime.UtcNow}] Authentication failed. Retrying after 60 seconds...(Attempt {retryCount} of 10)")))
.ExecuteAndCapture(() => AuthenticationManager.Authenticate())
.Result;
为什么CustomException
没有从后备动作中抛出?正确的方法应该是什么?