How to handle exceptions in Linked targets?
-
Monday, December 31, 2012 1:28 PM
I'm working on a lightweight message bus using TPL Dataflow. You can see the full implementation at https://gist.github.com/4416655.
One thing I'm unsure of is where to handle exceptions thrown by target blocks. It looks like TDF takes care of ensuring other (non faulting) targets still receive the data.
I tried wrapping BroadcastBlock<T>.Complete() in a try/catch block but the exception is not caught so I'm assuming I'd need to do this within the target block?
public Guid Subscribe<TMessage>(Action<TMessage> handlerAction) { var handler = new ActionBlock<object>({ try { message => handlerAction((TMessage)message); } catch (Exception ex){ // log exception // add retry logic } }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 2 } ); var subscription = broadcast.LinkTo( handler, new DataflowLinkOptions { PropagateCompletion = true }, message => message is TMessage ); return AddSubscription(subscription); }
Thanks
- Edited by Ben Foster Monday, December 31, 2012 1:28 PM
- Edited by Ben Foster Monday, December 31, 2012 1:29 PM
- Edited by Ben Foster Monday, December 31, 2012 1:44 PM
All Replies
-
Sunday, January 06, 2013 2:40 PM
Exceptions in TDF never propagate backwards (from target to source), only forwards (from source to target) and only if you set PropagateCompletion.
Also, Complete() doesn't throw an exception, you need to access the Completion Task for that.
- Proposed As Answer by Stephen Toub - MSFTMicrosoft Employee, Owner Tuesday, January 08, 2013 6:48 PM
- Marked As Answer by Stephen Toub - MSFTMicrosoft Employee, Owner Thursday, January 17, 2013 1:04 AM

