コード例 #1
0
        private void PrepareSplitter(Splitter current, object obj)
        {
            var nextAction       = current.Next?.ProcessAction;
            var afterSplitAction = current.ConnectedCollector?.Next?.ProcessAction;

            if (current.IsAsync)
            {
                var nextAsyncProcessor  = ProcessingElement.NextAsyncProcessor(current.Next);
                var afterAsyncProcessor = ProcessingElement.NextAsyncProcessor(current.ConnectedCollector?.Next);
                current.ProcessTask = async(tctx, octx) =>
                {
                    try
                    {
                        var parameters           = PrepareParameters(current, octx, tctx);
                        var splitterInvokeResult = current.Method.Invoke(obj, parameters.ToArray());
                        var result = typeof(IEnumerable <UnitContext>).IsAssignableFrom(splitterInvokeResult.GetType())
                            ? ((IEnumerable <UnitContext>)splitterInvokeResult).GetEnumerator()
                            : (IEnumerator <UnitContext>)splitterInvokeResult;

                        var dict = new Dictionary <string, IList <UnitContext> >();

                        while (result.MoveNext())
                        {
                            var soctx = result.Current;
                            nextAction?.Invoke(tctx, soctx);
                            if (tctx.Exception != null)
                            {
                                throw tctx.Exception;
                            }

                            if (nextAsyncProcessor != null)
                            {
                                await nextAsyncProcessor.ProcessTask(tctx, soctx);
                            }

                            current.ConnectedCollector.CollectAction(tctx, soctx, dict);
                        }

                        SetCollectedUnit(current, octx, dict);

                        afterSplitAction?.Invoke(tctx, octx);
                        if (afterAsyncProcessor != null)
                        {
                            await afterAsyncProcessor.ProcessTask(tctx, octx);
                        }
                    }
                    catch (PipelineProcessingException)
                    {
                        throw;
                    }
                    catch (Exception exception)
                    {
                        var resultException = ProcessException(current, tctx, exception, octx, obj);
                        throw new PipelineProcessingException(resultException);
                    }
                };
            }
            else
            {
                current.ProcessAction = (tctx, octx) =>
                {
                    try
                    {
                        var parameters = PrepareParameters(current, octx, tctx);
                        var result     = (IEnumerator <UnitContext>)current.Method.Invoke(obj, parameters.ToArray());
                        var dict       = new Dictionary <string, IList <UnitContext> >();

                        while (result.MoveNext())
                        {
                            var soctx = result.Current;
                            nextAction?.Invoke(tctx, soctx);
                            if (tctx.Exception != null)
                            {
                                throw tctx.Exception;
                            }

                            current.ConnectedCollector.CollectAction(tctx, soctx, dict);
                        }

                        SetCollectedUnit(current, octx, dict);
                        afterSplitAction?.Invoke(tctx, octx);
                    }
                    catch (PipelineProcessingException)
                    {
                        throw;
                    }
                    catch (Exception exception)
                    {
                        var resultException = ProcessException(current, tctx, exception, octx, obj);
                        throw new PipelineProcessingException(resultException);
                    }
                };
            }
        }
コード例 #2
0
        private static void PrepareProcessor(Processor current, object obj)
        {
            var nextAction = current.Next?.ProcessAction;

            if (current.IsAsync)
            {
                var nextAsyncProcessor = ProcessingElement.NextAsyncProcessor(current.Next);
                current.ProcessTask = async(tctx, octx) =>
                {
                    ProcessingInfo info = new ProcessingInfo
                    {
                        Started  = DateTime.Now,
                        StepName = !string.IsNullOrEmpty(current.Name) ? current.Name : current.Method.Name
                    };

                    tctx.ProcessingHistory.Push(info);

                    try
                    {
                        var parameters = PrepareParameters(current, octx, tctx);

                        if (current.Method.ReturnType.IsGenericType &&
                            current.Method.ReturnType.GetGenericTypeDefinition() == typeof(Task <>))
                        {
                            octx.Unit =
                                await Wrap(current.Method.Invoke(obj ?? current.ConcreteObject, parameters.ToArray()));
                        }

                        if (current.Method.ReturnType == typeof(Task))
                        {
                            await(Task) current.Method.Invoke(obj ?? current.ConcreteObject, parameters.ToArray());
                        }

                        info.Finished = DateTime.Now;

                        nextAction?.Invoke(tctx, octx);

                        if (nextAsyncProcessor != null)
                        {
                            await nextAsyncProcessor.ProcessTask(tctx, octx);
                        }
                    }
                    catch (PipelineProcessingException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        Exception resultException = ProcessException(current, tctx, e, octx, obj, true);
                        throw new PipelineProcessingException(resultException);
                    }
                };
            }
            else
            {
                current.ProcessAction = (tctx, octx) =>
                {
                    ProcessingInfo info = new ProcessingInfo
                    {
                        Started  = DateTime.Now,
                        StepName = !string.IsNullOrEmpty(current.Name) ? current.Name : current.Method.Name
                    };

                    tctx.ProcessingHistory.Push(info);

                    try
                    {
                        var parameters = PrepareParameters(current, octx, tctx);

                        if (current.Method.ReturnType == typeof(void))
                        {
                            current.Method.Invoke(obj ?? current.ConcreteObject, parameters.ToArray());
                        }
                        else
                        {
                            octx.Unit = current.Method.Invoke(obj ?? current.ConcreteObject, parameters.ToArray());
                        }

                        info.Finished = DateTime.Now;

                        nextAction?.Invoke(tctx, octx);
                    }
                    catch (PipelineProcessingException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        var resultException = ProcessException(current, tctx, e, octx, obj, true);
                        if (resultException != null)
                        {
                            throw new PipelineProcessingException(resultException);
                        }
                    }
                    finally
                    {
                        info.Finished = DateTime.Now;
                    }
                };
            }
        }