艺海无涯,学无止境。欢迎一起探讨技术和人生,了解我

Policy组件根据方法返回结果进行重试

Policy组件根据方法返回结果进行重试,这里,判断返回不为空的时候,会进行重试,以0作为被除数来测试

        static void Main()
        {
            var retry = Policy.HandleResult(x => x != "").WaitAndRetry(new[] {TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(3),TimeSpan.FromSeconds(5)}, (result, count) =>
            {
                Console.WriteLine(count+" "+result.Result+" "+DateTime.Now);
            });

            retry.Execute(() => Test());
        }

        static string Test()
        {
            try
            {
                int a = 0;
                int b = 1 / a;
               
            }
            catch (Exception e)
            {
                return e.Message;
            }

            return "";
        }

相关的文章