简体   繁体   English

TPL数据流。 无法将BatchBlock附加到ActionBlock。 无法从用法中推断出类型实参?

[英]TPL Dataflow. Cannot attach BatchBlock to ActionBlock. Type argument cannot be inferred from usage?

I have this batch block as 我有这个批处理块

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

And this ActionBlock as 而这个ActionBlock作为

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 我收到一个错误消息,说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 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 但是,编译器不会抱怨,而不是在构造器中附加一个创建新的ActionBlock ,例如

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 ??? 为什么不让我将actionBlock附加到batchBlock

The type signature for the LinkTo extension method should shed some light on what's wrong: LinkTo扩展方法的类型签名应该可以弄清问题所在:

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> . 您要指定BatchBlock<T>作为源,并指定ActionBlock<T>作为目标,通用类型TIEnumerable<string> However, the former implements ISourceBlock<T[]> , whilst the latter implements ITargetBlock<T> ; 但是,前者实现ISourceBlock<T[]> ,而后者实现ITargetBlock<T> thus, they are incompatible for being specified as arguments to the LinkTo method, which expects them to have the same type. 因此,它们被指定为LinkTo方法的参数是不兼容的, LinkTo方法期望它们具有相同的类型。

To fix this, you need to change your signature (and implementation) of actionBlock to: 要解决此问题,您需要将actionBlock签名(和实现) actionBlock为:

var actionBlock = new ActionBlock<IEnumerable<string>[]>(enumerables => ...
                                                  // ^ accept an array of enumerables 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM