在重试X后,Polly继续

时间:2016-05-07 19:05:45

标签: c# .net xamarin polly

我在一个非常棒的Xamarin项目中使用Polly。我面临的问题是,在2次重试之后它应该继续该方法,但由于某种原因它会卡住并继续重试。有谁知道我怎么能这样做?

private async Task<List<EventDto>> GetEventsErrorRemoteAsync()
{
    List<EventDto> conferences = null;

    if (CrossConnectivity.Current.IsConnected) 
    {
        // Retrying a specific amount of times (5)
        // In this case will wait for
        //  1 ^ 2 = 2 seconds then
        //  2 ^ 2 = 4 seconds then
        //  3 ^ 2 = 8 seconds then
        //  4 ^ 2 = 16 seconds then
        //  5 ^ 2 = 32 seconds
        conferences = await Policy
            .Handle<Exception>()
            .WaitAndRetryAsync(
                retryCount: 2, 
                sleepDurationProvider:retryAttempt => 
                    TimeSpan.FromSeconds(Math.Pow (2, retryAttempt)), 
                onRetry: (exception, timeSpan, context) => 
                {
                    var ex = (ApiException)exception;

                    //Do something with exception. Send to insights (now hockeyapp)
                    Debug.WriteLine($"Error: {ex.ReasonPhrase} | 
                        TimeSpan: {timeSpan.Seconds}");
                    return;
                }).ExecuteAsync(async () => 
                    await _eventService.GetEventsWithError());
    }
    return conferences;
}

1 个答案:

答案 0 :(得分:1)

问题详细回答了相同的Github问题:https://github.com/App-vNext/Polly/issues/106

政策不会因保持重试而停滞不前。相反,如果重试策略在.ExecuteAsync()内执行委托时所做的最后尝试仍然抛出,则重试策略将重新抛出此最终异常。由于您没有捕获并处理该异常,因此该方法自然不会继续返回语句。