来自 console.writeline 的内容不适用于 Polly

问题描述

目标:
3 次尝试后显示消息“显示错误”。

问题:
为了实现目标,代码的哪一部分不起作用。

while

谢谢!

解决方法

等待您的任务首先运行,您的策略也表明在 HttpRequestExceptionTaskCanceledException 的情况下重试策略将起作用并且您的方法没有任何例外。

如果您想测试重试策略,您可以执行以下操作:

public static async Task Main(string[] args)
{
    var maxRetryAttempts = 3;
    var pauseBetweenFailures = TimeSpan.FromSeconds(2);

    await Policy
        .Handle<HttpRequestException>()
        .Or<Exception>() //if any exception raised will try agian
        .Or<TaskCanceledException>()
        .WaitAndRetryAsync(maxRetryAttempts,i => pauseBetweenFailures)
        .ExecuteAsync( PersistApplicationData2)
        .ContinueWith(x =>
        {
            if (x.Exception != null)
            {
                Console.WriteLine("show error");
            }
            //success
        },scheduler: TaskScheduler.Default);
}