I am using TaskParallelLibrary DataFlow
combined with Try
library designed by Stephen Cleary (https://github.com/StephenCleary/Try) to achieve what is called "railroad programming" so I could pass Exception
data down the pipe. I would like to know to if it is somehow possible in ActionBlock
to get some context of what or (in my case) exactly which item caused the Exception
?
Here is small sample code:
public async Task TestRailroadException(List<int> constructIds)
{
var downloadBlock = new TransformBlock<int, Try<int>>(
construct => Try.Create(() =>
{
//ThisMethodMyThrowException();
return 1;
}));
var processBlock = new TransformBlock<Try<int>, Try<int>>(
construct => construct.Map(value =>
{
//ThisMethodMyAlsoThrowException();
return 1;
}));
var resultsBlock = new ActionBlock<Try<int>>(construct =>
{
if (construct.IsException)
{
var type = construct.Exception.GetType();
//Here it would be nice to know which item(id) was faulted.
}
});
downloadBlock.LinkTo(processBlock, new DataflowLinkOptions
{ PropagateCompletion = true });
processBlock.LinkTo(resultsBlock, new DataflowLinkOptions
{ PropagateCompletion = true });
foreach (var constructId in constructIds)
{
await downloadBlock.SendAsync(constructId);
}
downloadBlock.Complete();
await resultsBlock.Completion;
}