private TransformBlock <PipelineItem <TItem>, PipelineItem <TItem> > BuildProcessingBlock <TItem>( PipelineBlock <TItem> block, PipelineTaskStep <TItem> step, DataflowExecutionContext context, CancellationToken cancellation) { return(new TransformBlock <PipelineItem <TItem>, PipelineItem <TItem> >( x => { if (x == null) { return x; } context.Events?.Raise(e => e.OnBlockStarted(block, x.Number, x.Item, step, context.StepContext.Task)); var sw = Stopwatch.StartNew(); try { block.Body(x.Item); } catch (Exception e) { throw new TaskExecutionException(e, step, block.GetExceptionData(x.Number)); } sw.Stop(); context.TotalBlockDurations.AddOrUpdate(block.Name, sw.Elapsed, (_, duration) => duration + sw.Elapsed); context.Events?.Raise(e => e.OnBlockEnded(block, x.Number, x.Item, step, context.StepContext.Task, sw.Elapsed)); return x; }, new ExecutionDataflowBlockOptions { NameFormat = block.Name, MaxDegreeOfParallelism = block.MaxDegreeOfParallelism, BoundedCapacity = block.MaxDegreeOfParallelism, EnsureOrdered = true, CancellationToken = cancellation })); }