Beispiel #1
0
        public async Task Invoke(IncomingContext context)
        {
            int currentIndex = 0;
            var semaphore = new SemaphoreSlim(1);
            Stack<Tuple<Task, TaskCompletionSource<ExceptionDispatchInfo>>> sources = new Stack<Tuple<Task, TaskCompletionSource<ExceptionDispatchInfo>>>();

            while (currentIndex < executingElements.Count)
            {
                await semaphore.WaitAsync().ConfigureAwait(false);

                var element = executingElements[currentIndex];
                currentIndex += 1;

                var tcs = new TaskCompletionSource<ExceptionDispatchInfo>();
                var task = element.Invoke(context, () =>
                {
                    semaphore.Release();
                    return tcs.Task;
                });

                sources.Push(Tuple.Create(task, tcs));
            }

            ExceptionDispatchInfo exception = null;
            var anotherSemaphore = new SemaphoreSlim(1);
            var allTasks = new ConcurrentBag<Task>();
            foreach (var source in sources)
            {
                await anotherSemaphore.WaitAsync().ConfigureAwait(false);

                if (exception != null)
                {
                    context.Exceptions.Enqueue(exception);
                    source.Item2.TrySetException(exception.SourceException);
                    exception = null;
                }
                else
                {
                    source.Item2.TrySetResult(null);
                }

                var task = source.Item1.ContinueWith(t =>
                {
                    if (t.Exception != null)
                    {
                        exception = ExceptionDispatchInfo.Capture(t.Exception.InnerException);
                    }
                    anotherSemaphore.Release();
                }, TaskContinuationOptions.ExecuteSynchronously);
                allTasks.Add(task);
            }

            await Task.WhenAll(allTasks).ConfigureAwait(false);
        }
Beispiel #2
0
 public async Task Invoke(IncomingContext context, Func <Task> next)
 {
     try
     {
         await next();
     }
     catch (Exception e)
     {
         context.Exceptions.Dequeue().SourceException.StackTrace.Output();
     }
     countdown.Signal();
 }
Beispiel #3
0
 public Task Invoke(IncomingContext context, Func<Task> next)
 {
     return next();
 }
Beispiel #4
0
            public async Task Invoke(IncomingContext context, Func<Task> next)
            {
                await Task.Delay(10).ConfigureAwait(false);

                throw new InvalidOperationException(nameof(ThrowException));
            }
Beispiel #5
0
 public async Task Invoke(IncomingContext context, Func<Task> next)
 {
     await Task.Delay(10).ConfigureAwait(false);
     await next().ConfigureAwait(false);
 }
Beispiel #6
0
 static Task Connector(ChainFactory factory, TransportMessage message)
 {
     var pipeline = factory.Create();
     var context = new IncomingContext(message);
     return pipeline.Invoke(context);
 }
Beispiel #7
0
 public async Task Invoke(IncomingContext context, Func<Task> next)
 {
     try
     {
         await next();
     }
     catch (Exception e)
     {
         context.Exceptions.Dequeue().SourceException.StackTrace.Output();
     }
     countdown.Signal();
 }
Beispiel #8
0
            public async Task Invoke(IncomingContext context, Func<Task> next)
            {
                using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    await Task.Delay(10).ConfigureAwait(false);

                    await next().ConfigureAwait(false);

                    scope.Complete();
                }
            }
Beispiel #9
0
            public async Task Invoke(IncomingContext context, Func <Task> next)
            {
                await Task.Delay(10).ConfigureAwait(false);

                throw new InvalidOperationException(nameof(ThrowException));
            }
Beispiel #10
0
            public async Task Invoke(IncomingContext context, Func <Task> next)
            {
                await Task.Delay(10).ConfigureAwait(false);

                await next().ConfigureAwait(false);
            }
Beispiel #11
0
 public Task Invoke(IncomingContext context, Func <Task> next)
 {
     return(next());
 }