Пример #1
0
        public async Task ExecuteAsync(OperationContext context, CancellationToken cancellationToken)
        {
            var            result = default(TResult);
            var            executedSuccessfully = false;
            OperationError error;

            do
            {
                error = null;

                try
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (Condition == null || await Condition.CheckAsync(context, cancellationToken))
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        result = await _expression(cancellationToken).NotNull();

                        cancellationToken.ThrowIfCancellationRequested();
                        executedSuccessfully = true;
                    }
                    else
                    {
                        throw new OperationCanceledException(CancellationToken.None);
                    }
                }
                catch (OperationCanceledException ex)
                {
                    var cancelTask = CancelHandler(ex.CancellationToken).NotNull();
                    OnRequestHideNotification(new HideNotificationEventArgs(OperationCallback.Cancel));

                    await cancelTask;
                }
                catch (Exception ex)
                {
                    error = new OperationError(ex);
                    var errorHandler = GetErrorHandler(ex);
                    var errorTask    = errorHandler(error, cancellationToken).NotNull();
                    OnRequestHideNotification(new HideNotificationEventArgs(OperationCallback.Error));

                    await errorTask;

                    if (!error.Handled)
                    {
                        throw;
                    }
                }
            }while (error?.RetryOperation ?? false);

            if (executedSuccessfully)
            {
                var successTask = SuccessHandler(result, cancellationToken).NotNull();
                OnRequestHideNotification(new HideNotificationEventArgs(OperationCallback.Success));

                await successTask;
            }
        }
Пример #2
0
        internal async Task ExecuteAsync([NotNull] IOperation operation, CancellationToken cancellationToken)
        {
            var notificationCancellationTokenSource = operation.Notification?.CancellationTokenSource;
            var linkedCancellationTokenSource       = CancellationTokenSource.CreateLinkedTokenSource(
                notificationCancellationTokenSource?.Token ?? CancellationToken.None,
                cancellationToken);
            var linkedCancellationToken = linkedCancellationTokenSource.Token;

            try
            {
                OperationError error;

                do
                {
                    error = null;

                    var operationTask = operation.ExecuteAsync(this, linkedCancellationToken);
                    var notificationMinDurationTask = Task.CompletedTask;

                    if (operation.Notification != null)
                    {
                        var notificationDelayTask = operation.Notification.GetDelayTask();
                        var completedTask         = await Task.WhenAny(operationTask, notificationDelayTask).NotNull();

                        if (completedTask != operationTask)
                        {
                            operation.HideNotificationRequested += Operation_HideNotificationRequested;
                            RequestShowNotification(operation.Notification);
                            notificationMinDurationTask = operation.Notification.GetMinDurationTask();
                        }
                    }

                    try
                    {
                        await Task.WhenAll(operationTask, notificationMinDurationTask).NotNull();
                    }
                    catch (Exception ex)
                    {
                        error = new OperationError(ex);
                        await OnErrorAsync(error);

                        if (!error.Handled)
                        {
                            throw;
                        }
                    }
                }while (error?.RetryOperation ?? false);
            }
            finally
            {
                notificationCancellationTokenSource?.Dispose();
                linkedCancellationTokenSource.Dispose();
            }
        }
Пример #3
0
        public async Task <IOperationErrorResult> HandleAsync(OperationContext context, Exception ex, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (ex == null)
            {
                throw new ArgumentNullException(nameof(ex));
            }

            var error = new OperationError <TException>((TException)ex);

            await _handler(context, error, cancellationToken).NotNull();

            return(error);
        }
Пример #4
0
 protected virtual Task OnErrorAsync([NotNull] OperationError error)
 {
     return(Task.CompletedTask);
 }