How to handle exceptions in Linked targets?

Answered How to handle exceptions in Linked targets?

  • Monday, December 31, 2012 1:28 PM
     
      Has Code

    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