【问题标题】:Polly continue after X retries重试 X 次后 Polly 继续
【发布时间】:2016-05-08 22:52:13
【问题描述】:

我在 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;
}

【问题讨论】:

    标签: c# .net xamarin polly


    【解决方案1】:

    关于同一个 Github 问题的详细回答:https://github.com/App-vNext/Polly/issues/106

    策略没有卡住,一直在重试。相反,如果重试策略在.ExecuteAsync() 内执行委托的最终尝试仍然抛出,则重试策略将重新抛出这个最终异常。由于您没有捕获和处理该异常,因此该方法自然不会继续返回语句。

    【讨论】:

      猜你喜欢
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多