(The real title of the question should be "Why do I get a 'Unable to cast object of type 'System.Runtime.CompilerServices.TaskAwaiter`1[System.Runtime.CompilerServices.VoidTaskResult]' to type 'System.Runtime.CompilerServices.INotifyCompletion'", but unfortunately this is too long for StackOverflow. :)
Hi,
I'm getting really peculiar problems when trying to await
the execution of a method of mine. The calling code looks like this (excerpt):
private async Task DownloadAddonFileAsync(dynamic addon, dynamic file, string targetFolder)
{
// ...
await DownloadFileAsync(file, targetFolder, uri);
The DownloadFileAsync looks like this:
protected async Task DownloadFileAsync(dynamic file, string targetFolder, string uri)
{
// ...
var fileBytes = await AppLoaderRestClient.GetAsync<byte[]>(uri);
The AppLoaderRestClient.GetAsync(), in turn, looks like this:
public static async Task<T> GetAsync<T>(string uri)
{
// ...
if (typeof (T) == typeof (byte[]))
{
var result = await webClient.DownloadDataTaskAsync(uri);
return (T) (object) result;
}
So, there is actually a chain of Tasks here - the "inner" Task will be a Task, which will then be propagated up to the caller, and converted to a Task (i.e. a Task without a result). I presume this could be causing the issue at hand?
If I change the outermost code to this:
var task = DownloadFileAsync(file, targetFolder, uri);
task.Wait();
...it works flawlessly. Why?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…