Exemplo n.º 1
0
        public virtual Task <TResponse> RunAsync <TResponse>
        (
            ITasq <TResponse> tasq,
            CancellationToken cancellationToken = default
        )
        {
            Task <TResponse> retVal = null;

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(tasq));

            var resolvedHandler = GetHandlerDetail(tasq);

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(resolvedHandler.Handler));

            if (resolvedHandler.Handler is TasqHandler)
            {
                retVal = Task.FromResult(Run(tasq));
            }
            else
            {
                TasqHandlerAsync tasqHandlerInstance = (TasqHandlerAsync)resolvedHandler.Handler;

                tasqHandlerInstance.p_CancellationToken = cancellationToken;

                retVal = tasqHandlerInstance.InitializeAsync(tasq)
                         .ContinueWith(res1 =>
                {
                    if (res1.IsFaulted)
                    {
                        throw new TasqException(res1.Exception);
                    }

                    return(tasqHandlerInstance.BeforeRunAsync(tasq)
                           .ContinueWith(res2 =>
                    {
                        if (res2.IsFaulted)
                        {
                            throw new TasqException(res2.Exception);
                        }

                        return tasqHandlerInstance.XRunAsync(null, tasq)
                        .ContinueWith(res3 =>
                        {
                            if (res3.IsFaulted)
                            {
                                throw new TasqException(res3.Exception);
                            }

                            return tasqHandlerInstance.AfterRunAsync(tasq)
                            .ContinueWith(res4 =>
                            {
                                if (res4.IsFaulted)
                                {
                                    throw new TasqException(res4.Exception);
                                }

                                return (Task <TResponse>)res3;
                            }).Unwrap();
                        }).Unwrap();
                    }).Unwrap());
                }).Unwrap();
            }

            return(retVal);
        }
Exemplo n.º 2
0
        public virtual Task <IEnumerable <TResponse> > RunAsync <TKey, TResponse>
        (
            ITasq <TKey, TResponse> tasq,
            CancellationToken cancellationToken = default
        )
        {
            Task <IEnumerable <TResponse> > retVal = null;

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(tasq));

            var resolvedHandler = GetHandlerDetail(tasq);

            OnLog?.Invoke(this, TasqProcess.Start, new LogEventHandlerEventArgs(resolvedHandler.Handler));

            if (resolvedHandler.Handler is TasqHandler)
            {
                retVal = Task.FromResult(Run(tasq));
            }
            else
            {
                TasqHandlerAsync tasqHandlerInstance = (TasqHandlerAsync)resolvedHandler.Handler;

                retVal = tasqHandlerInstance.InitializeAsync(tasq)
                         .ContinueWith(async res1 =>
                {
                    if (res1.IsFaulted)
                    {
                        throw new TasqException(res1.Exception);
                    }

                    List <TResponse> abRet = new List <TResponse>();

                    var selectionCriteria = await tasqHandlerInstance.SelectionCriteriaAsync(tasq);

                    List <Task <TResponse> > listOfTask = new List <Task <TResponse> >();

                    foreach (var item in selectionCriteria)
                    {
                        var task = tasqHandlerInstance.BeforeRunAsync(tasq)
                                   .ContinueWith(res2 =>
                        {
                            if (res2.IsFaulted)
                            {
                                throw new TasqException(res2.Exception);
                            }

                            return(tasqHandlerInstance.XRunAsync(item, tasq)
                                   .ContinueWith(res3 =>
                            {
                                if (res3.IsFaulted)
                                {
                                    throw new TasqException(res3.Exception);
                                }

                                return tasqHandlerInstance.AfterRunAsync(tasq)
                                .ContinueWith(res5 =>
                                {
                                    if (res5.IsFaulted)
                                    {
                                        throw new TasqException(res5.Exception);
                                    }

                                    var tasR3 = (Task <TResponse>)res3;

                                    abRet.Add(tasR3.Result);

                                    return tasR3;
                                }).Unwrap();
                            }, TaskContinuationOptions.AttachedToParent).Unwrap());
                        }, TaskContinuationOptions.AttachedToParent).Unwrap();

                        listOfTask.Add(task);
                    }

                    var allResult = await Task.WhenAll(listOfTask);

                    return(allResult.AsEnumerable());
                }).Unwrap();
            }

            return(retVal);
        }