Пример #1
0
        public void RethrowStack()
        {
            var options = MultiTryOptions <int> .Default;

            options.OnFinalFailure = ex =>
            {
                MultiTry.Rethrow(ex);
                return(default(int));
            };

            var zero = 0;

            try
            {
                MultiTry.Try(() => 1 / zero, options);
            }
            catch (DivideByZeroException dEx)
            {
                Trace.WriteLine(dEx);
                Assert.IsTrue(dEx.ToString().Contains("ExceptionDispatchInfo"));
            }

            options = MultiTryOptions <int> .Default;
            options.OnFinalFailure = ex => throw ex;
            Trace.WriteLine("");
            try
            {
                MultiTry.Try(() => 1 / zero, options);
            }
            catch (DivideByZeroException dEx)
            {
                Trace.WriteLine(dEx);
                Assert.IsFalse(dEx.ToString().Contains("ExceptionDispatchInfo"));
            }
        }
Пример #2
0
        private async Task <string> GetPage()
        {
            //nonexisting address
            _uri = new Uri("http://bad.example.org");

            var options = MultiTryOptions <string> .Default;

            //set delay between attempts to 100 ms
            options.Delay = 100;

            //if we get any other exception besides WebException, we don't want to retry
            options.ExceptionFilter = ex =>
            {
                Trace.WriteLine($"Exception: {ex}");
                //true means "handle exception", false "don't handle at all"
                return(ex.GetType() == typeof(WebException));
            };

            //try up to 3 times
            options.TryCount = 3;

            //when we get to error handler, let's change URL to correct one
            options.OnException = (exception, i) =>
            {
                _uri = new Uri("http://example.org");
                Trace.WriteLine($"Attempt {i}: {exception.Message}. Changing URL to {_uri}");

                //returning true continues with next attempt, false breaks out of retry loop and executes OnFinalFailure immediately
                return(true);
            };

            //if all attempts have failed, let's rethrow the exception, with proper stacktrace
            options.OnFinalFailure = ex =>
            {
                MultiTry.Rethrow(ex);
                return(null);                                                                 //make compiler happy
            };

            var page = await MultiTry.TryAsync(() => DownloadPage(), options);

            Trace.WriteLine(page);
            return(page);
        }
Пример #3
0
        public void BasicRunAsync()
        {
            var options = MultiTryOptions <bool> .Default;

            options.OnException = (exception, i) =>
            {
                Trace.WriteLine($"{i}: {exception.Message}");
                Assert.IsNotNull(exception);
                Assert.IsTrue(i >= 0 && i < 3);
                return(false);
            };
            options.Delay = 0;

            var func = new Func <Task <bool> >(async() =>
            {
                await Task.Delay(100);
                throw new ApplicationException();
            });

            var result = MultiTry.TryAsync(func, options).Result;

            Assert.IsFalse(result);
        }
Пример #4
0
 public void NullFunc()
 {
     MultiTry.Try <bool>(null);
 }