Beispiel #1
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));
            }
        }
Beispiel #2
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));
            }
        }
Beispiel #3
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)));
            }
        }
Beispiel #4
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)));
            }
        }
Beispiel #5
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)));
            }
        }
Beispiel #6
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)));
            }
        }
Beispiel #7
0
        public PolicyResult <TResult> ExecuteAndCapture <TResult>(Func <Context, CancellationToken, TResult> 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
            {
                return(PolicyResult <TResult> .Successful(Execute(action, context, cancellationToken), context));
            }
            catch (Exception exception)
            {
                return(PolicyResult <TResult> .Failure(exception, GetExceptionType(_exceptionPredicates, exception), context));
            }
        }
Beispiel #8
0
        public async Task <PolicyResult <TResult> > ExecuteAndCaptureAsync <TResult>(Func <CancellationToken, Task <TResult> > action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
        {
            if (_asyncExceptionPolicy == 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
            {
                return(PolicyResult <TResult> .Successful(await ExecuteAsync(action, context, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext)));
            }
            catch (Exception exception)
            {
                return(PolicyResult <TResult> .Failure(exception, GetExceptionType(_exceptionPredicates, exception)));
            }
        }
Beispiel #9
0
        public async Task <PolicyResult <TResult> > ExecuteAndCaptureAsync <TResult>(Func <CancellationToken, Task <TResult> > action, 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
            {
                var result = default(TResult);
                await _asyncExceptionPolicy(async ct =>
                {
                    result = await action(ct).ConfigureAwait(continueOnCapturedContext);
                }, cancellationToken, continueOnCapturedContext)
                .ConfigureAwait(continueOnCapturedContext);

                return(PolicyResult <TResult> .Successful(result));
            }
            catch (Exception exception)
            {
                return(PolicyResult <TResult> .Failure(exception, GetExceptionType(_exceptionPredicates, exception)));
            }
        }