Pregunta

I have this batch block as

var batchBlock = new BatchBlock<IEnumerable<string>>(10000,
                                                           new GroupingDataflowBlockOptions
                                                               {
                                                                CancellationToken = cancellationToken,
                                                                TaskScheduler = taskScheduler
                                                               }
                                                     );

And this ActionBlock as

var actionBlock = new ActionBlock<IEnumerable<string>>(enumerable =>
                                                       {

                                                           for (var i = 0;
                                                                i < enumerable.Count();
                                                                i++)
                                                           {
                                                               File.AppendAllText(@"D:\Test.log",
                                                                 enumerable.ElementAt(i));

                                                           }
                                                       });

When I try to attach them as

batchBlock.LinkTo(actionBlock);

I get an error saying The type arguments for method System.Threading.Tasks.Dataflow.DataflowBlock.LinkTo<TOutput>(System.Threading.Tasks.Dataflow.ISourceBlock<TOutput>, System.Threading.Tasks.Dataflow.ITargetBlock<TOutput>)' cannot be inferred from the usage. Try specifying the type arguments explicitly

and I get this very same error even when I do this..

batchBlock.LinkTo((ActionBlock<IEnumerable<string>>)actionBlock);

However, the compiler doesn't complains when instead of attaching a create a new ActionBlock in the constructor` such as

batchBlock.LinkTo(new ActionBlock<IEnumerable<string>[]>(enumerable => { /* log to file */ }));

Can anyone tell me what am I doing wrong? Why is it not letting me attach my actionBlock to batchBlock???

¿Fue útil?

Solución

The type signature for the LinkTo extension method should shed some light on what's wrong:

LinkTo<TOutput>(ISourceBlock<TOutput>, ITargetBlock<TOutput>)

You're specifying a BatchBlock<T> as the source, and an ActionBlock<T> as the target, with the generic type T being IEnumerable<string>. However, the former implements ISourceBlock<T[]>, whilst the latter implements ITargetBlock<T>; thus, they are incompatible for being specified as arguments to the LinkTo method, which expects them to have the same type.

To fix this, you need to change your signature (and implementation) of actionBlock to:

var actionBlock = new ActionBlock<IEnumerable<string>[]>(enumerables => ...
                                                  // ^ accept an array of enumerables 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top