TPL Dataflow, how to forward items to only one specific target block among many linked target blocks?

后端 未结 2 730
粉色の甜心
粉色の甜心 2020-12-17 09:40

I am looking for a TPL data flow block solution which can hold more than a single item, which can link to multiple target blocks, but which has the ability to forward an ite

2条回答
  •  庸人自扰
    2020-12-17 09:54

    I've found the accepted answer to be incorrect. The NullTarget should be linked with its predicate being the negation of your consumers. Otherwise you might drop messages that you wanted to consume.

    var forwarder = new BufferBlock();
    forwarder.LinkTo(target1, item => matchesTarget1(item));
    forwarder.LinkTo(target2, item => matchesTarget2(item));
    forwarder.LinkTo(DataflowBlock.NullTarget(), item => !matchesTarget1(item) && !matchesTarget2(item));
    

提交回复
热议问题