예제 #1
0
        public PolicyResult <TResult> ExecuteAndCapture(Func <Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
        {
            if (_executionPolicy == null)
            {
                throw new InvalidOperationException(
                          "Please use the synchronous-defined policies when calling the synchronous Execute (and similar) methods.");
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            try
            {
                TResult result = Execute(action, context, cancellationToken);

                if (ResultPredicates.Any(predicate => predicate(result)))
                {
                    return(PolicyResult <TResult> .Failure(result, context));
                }

                return(PolicyResult <TResult> .Successful(result, context));
            }
            catch (Exception exception)
            {
                return(PolicyResult <TResult> .Failure(exception, GetExceptionType(ExceptionPredicates, exception), context));
            }
        }
예제 #2
0
        public async Task <PolicyResult <TResult> > ExecuteAndCaptureAsync(Func <Context, CancellationToken, Task <TResult> > action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
        {
            if (_asyncExecutionPolicy == null)
            {
                throw new InvalidOperationException(
                          "Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.");
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            try
            {
                TResult result = await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);

                if (ResultPredicates.Any(predicate => predicate(result)))
                {
                    return(PolicyResult <TResult> .Failure(result, context));
                }

                return(PolicyResult <TResult> .Successful(result, context));
            }
            catch (Exception exception)
            {
                return(PolicyResult <TResult> .Failure(exception, GetExceptionType(ExceptionPredicates, exception), context));
            }
        }
예제 #3
0
        public async Task <PolicyResult <TResult> > ExecuteAndCaptureAsync <TResult>(Func <Task <TResult> > action, bool continueOnCapturedContext)
        {
            if (_asyncExceptionPolicy == null)
            {
                throw new InvalidOperationException(
                          "Please use the asynchronous RetryAsync, RetryForeverAsync, WaitAndRetryAsync or CircuitBreakerAsync methods when calling the asynchronous Execute method.");
            }

            try
            {
                var result = default(TResult);

                await _asyncExceptionPolicy(async() =>
                {
                    result = await action().ConfigureAwait(continueOnCapturedContext);
                }
                                            , continueOnCapturedContext)
                .ConfigureAwait(continueOnCapturedContext);

                return(PolicyResult <TResult> .Successful(result));
            }
            catch (Exception exception)
            {
                return(PolicyResult <TResult> .Failure(exception, GetExceptionType(_exceptionPredicates, exception)));
            }
        }
예제 #4
0
        public PolicyResult <TResult> ExecuteAndCapture <TResult>(Func <Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            try
            {
                return(PolicyResult <TResult> .Successful(Execute(action, context, cancellationToken), context));
            }
            catch (Exception exception)
            {
                return(PolicyResult <TResult> .Failure(exception, GetExceptionType(ExceptionPredicates, exception), context));
            }
        }
예제 #5
0
        public async Task <PolicyResult <TResult> > ExecuteAndCaptureAsync <TResult>(Func <Context, CancellationToken, Task <TResult> > action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            try
            {
                return(PolicyResult <TResult> .Successful(
                           await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext), context));
            }
            catch (Exception exception)
            {
                return(PolicyResult <TResult> .Failure(exception, GetExceptionType(ExceptionPredicates, exception), context));
            }
        }
예제 #6
0
        protected PolicyResult ExecuteAndCapture(Action action, Context context)
        {
            if (_exceptionPolicy == null)
            {
                throw new InvalidOperationException(
                          "Please use the synchronous Retry, RetryForever, WaitAndRetry or CircuitBreaker methods when calling the synchronous ExecuteAndCapture method.");
            }

            try
            {
                _exceptionPolicy(action, context);
                return(PolicyResult.Successful());
            }
            catch (Exception exception)
            {
                return(PolicyResult.Failure(exception, GetExceptionType(_exceptionPredicates, exception)));
            }
        }
예제 #7
0
        protected PolicyResult <TResult> ExecuteAndCapture <TResult>(Func <TResult> action, Context context)
        {
            if (_exceptionPolicy == null)
            {
                throw new InvalidOperationException(
                          "Please use the synchronous Retry, RetryForever, WaitAndRetry or CircuitBreaker methods when calling the synchronous ExecuteAndCapture method.");
            }

            try
            {
                var result = default(TResult);
                _exceptionPolicy(() => { result = action(); }, context);
                return(PolicyResult <TResult> .Successful(result));
            }
            catch (Exception exception)
            {
                return(PolicyResult <TResult> .Failure(exception, GetExceptionType(_exceptionPredicates, exception)));
            }
        }
예제 #8
0
        public async Task <PolicyResult> ExecuteAndCaptureAsync(Func <Task> action)
        {
            if (_asyncExceptionPolicy == null)
            {
                throw new InvalidOperationException
                          ("Please use the asynchronous RetryAsync, RetryForeverAsync, WaitAndRetryAsync or CircuitBreakerAsync methods when calling the asynchronous Execute method.");
            }

            try
            {
                await _asyncExceptionPolicy(action).NotOnCapturedContext();

                return(PolicyResult.Successful());
            }
            catch (Exception exception)
            {
                return(PolicyResult.Failure(exception, GetExceptionType(_exceptionPredicates, exception)));
            }
        }
예제 #9
0
        protected async Task <PolicyResult> ExecuteAndCaptureAsync(Func <CancellationToken, Task> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
        {
            if (_asyncExceptionPolicy == null)
            {
                throw new InvalidOperationException
                          ("Please use the asynchronous RetryAsync, RetryForeverAsync, WaitAndRetryAsync or CircuitBreakerAsync methods when calling the asynchronous Execute method.");
            }

            try
            {
                await _asyncExceptionPolicy(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);

                return(PolicyResult.Successful());
            }
            catch (Exception exception)
            {
                return(PolicyResult.Failure(exception, GetExceptionType(_exceptionPredicates, exception)));
            }
        }
예제 #10
0
        public PolicyResult ExecuteAndCapture(Action <Context, CancellationToken> action, Context context, CancellationToken cancellationToken)
        {
            if (_exceptionPolicy == null)
            {
                throw new InvalidOperationException(
                          "Please use the synchronous-defined policies when calling the synchronous Execute (and similar) methods.");
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            try
            {
                Execute(action, context, cancellationToken);
                return(PolicyResult.Successful(context));
            }
            catch (Exception exception)
            {
                return(PolicyResult.Failure(exception, GetExceptionType(_exceptionPredicates, exception), context));
            }
        }