コード例 #1
0
        public static Task <dynamic> Verify(this Task <dynamic> request,
                                            Func <dynamic, bool> verify,
                                            CancellationToken cancellationToken         = default,
                                            TaskContinuationOptions continuationOptions = TaskContinuationOptions.None,
                                            TaskScheduler scheduler = null)
        {
            var result = request.AsyncState ?? request.Result; // In the case of a faulted task and use the first to verify the result

            dynamic ContinuationFunction(Task <dynamic> task, dynamic state)
            {
                if (verify == null || verify(state) == true)
                {
                    if (task.IsFaulted)
                    {
                        throw task.Exception.ToFlatAggregateException();
                    }

                    return(state);
                }

                var message        = DalSoft.RestClient.Verify.FailedMessage(verify);
                var verifiedFailed = new VerifiedFailed(message);

                throw task.Exception.ToFlatAggregateException(verifiedFailed);
            }

            return(request.ContinueWith
                   (
                       continuationFunction: (Func <Task <dynamic>, object, object>)((task, state) => ContinuationFunction(task, state)),
                       cancellationToken: cancellationToken,
                       continuationOptions: continuationOptions,
                       scheduler: scheduler ?? TaskScheduler.Default,
                       state: result
                   ));
        }
コード例 #2
0
        public static Task <dynamic> Verify <TResponse>(this Task <dynamic> request,
                                                        Expression <Func <TResponse, bool> > verify,
                                                        CancellationToken cancellationToken         = default,
                                                        TaskContinuationOptions continuationOptions = TaskContinuationOptions.None,
                                                        TaskScheduler scheduler = null) where TResponse : class
        {
            var result = request.AsyncState ?? request.Result; // In the case of a faulted task and use the first to verify the result

            dynamic ContinuationFunction(Task <dynamic> task, dynamic state)
            {
                var response = typeof(TResponse) == typeof(string) ? state?.ToString() : (TResponse)state;

                if (verify == null || verify.Compile()(response) == true)
                {
                    if (task.IsFaulted)
                    {
                        throw task.Exception.ToFlatAggregateException();
                    }

                    return(state);
                }


                var message        = DalSoft.RestClient.Verify.FailedMessage(verify);
                var verifiedFailed = new VerifiedFailed(message);

                throw task.Exception.ToFlatAggregateException(verifiedFailed);
            }

            return(request.ContinueWith
                   (
                       continuationFunction: (Func <Task <dynamic>, object, object>)ContinuationFunction,
                       cancellationToken: cancellationToken,
                       continuationOptions: continuationOptions,
                       scheduler: scheduler ?? TaskScheduler.Default,
                       state: result
                   ));
        }