TLDR;
Why is the first call here ambiguous?
is there a way for me to explain to the compiler the ambiguity without creating a new Func?
When using DataFlow (TPL) one can create an ActionBlock as so:
var downloadString = new TransformBlock<string, string>(uri =>
{
Console.WriteLine("Downloading '{0}'...", uri);
return new WebClient().DownloadString(uri);
});
TransformBlock has (among others) two constructors that look like this:
public TransformBlock(Func<TInput, TOutput> transform);
public TransformBlock(Func<TInput, Task<TOutput>> transform);
thus you can create asynchronous blocks:
var downloadString = new TransformBlock<string, string>(async uri =>
{
Console.WriteLine("Downloading '{0}'...", uri);
return await (new WebClient().DownloadStringAsync(uri));
});
I'm however having issues when I want to move the lambda to it's own Method, i.e.
...
var downlodString = new TransformBlock<string, string>(DownloadUriAsync);
...
private async Task<string> DownloadUriAsync(string uri){
Console.WriteLine("Downloading '{0}'...", uri);
return await (new WebClient().DownloadStringAsync(uri));
}
error CS0121: The call is ambiguous between the following methods or properties:
TransformBlock<TInput, TOutput>.TransformBlock(Func<TInput, TOutput>)
andTransformBlock<TInput, TOutput>.TransformBlock(Func<TInput, Task<TOutput>>)
I can't really see why it is ambiguous, since I have defined the generic parameters as <string, string>
and not <string, Task<string>>
. I can do a workaround as:
var downlodString = new TransformBlock<string, string>(new Func<string, Task<string>>(DownloadUriAsync));
But that feels a bit clunky to me.
So to repeat the questions:
Why is the call ambiguous in the first place?
is there a way for me to explain to the compiler the ambiguity without creating a new Func?