Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Error 'Cannot await 'System.Threading.Tasks.Task' from await and async properties

I have installed the NuGet Package Async for .NET Framework 4, Silverlight 4 and 5, and Windows Phone 7.5 and 8.

Version 1.0.16

I am using Microsoft .NET 4.0 and I can not upgrade due to Web Hosting Package Restrictions. (Sadly!)

My test Code: (I am doing a Linq to SQL Query in place of Thread.Sleep)

public class Search
{
public async Task<List<Result>> GetResults(string SearchString)
{
await System.Threading.Tasks.Task.Factory.StartNew(() => Thread.Sleep(1000));
}
}

My Error:

Cannot await 'System.Threading.Tasks.Task'

http://blogs.msdn.com/b/bclteam/p/asynctargetingpackkb.aspx talks about the error but it is not really the answer as I am not using VB and I am already doing what the solution describes.

[EDIT] I am getting a warning:

Warning 4   The primary reference "Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" could not be resolved because it has an indirect dependency on the framework assembly "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" or retarget your application to a framework version which contains "System.Runtime, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".

Thanks for your help.

like image 647
Rusty Nail Avatar asked Oct 18 '13 00:10

Rusty Nail


People also ask

What is async await and Task in C#?

The async keyword turns a method into an async method, which allows you to use the await keyword in its body. When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete.

What is the difference between Task and async await?

Async methods are intended to be non-blocking operations. An await expression in an async method doesn't block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.

Does await Block C#?

When the await operator is applied to the operand that represents an already completed operation, it returns the result of the operation immediately without suspension of the enclosing method. The await operator doesn't block the thread that evaluates the async method.

What is async and await in .NET core?

The async and await keywordsAn asynchronous method is one that is marked with the async keyword in the method signature. It can contain one or more await statements. It should be noted that await is a unary operator — the operand to await is the name of the method that needs to be awaited.


1 Answers

What a Nightmare! I have found the problem.

Microsoft.Bcl

It appears in my case that the Microsoft.Bcl and Microsoft.Bcl.Async are in conflict. I removed the Microsoft.Bcl and Microsoft.Bcl.Async with the following commands:

uninstall-package Microsoft.Bcl.Async -force

then:

uninstall-package Microsoft.Bcl -force

and then install again:

install-package Microsoft.Bcl.Async

Now it works a charm. I cant believe this? Also not sure as to why Microsoft.Bcl was left there in the first place. After removing Microsoft.Bcl.Async it gets left behind. So far all works fine. Some more testing to be done.

If others have issues with NuGet Packages, you can try at your own risk:

update-package -pre

This will try to roll back packages that have been updated. This is how I found the issue.

[EDIT] Its worth noting after further testing, Microsoft.Bcl is not removed when one uninstalls Microsoft.Bcl.Async. Microsoft.Bcl can be updated separately and it appears this update outside Microsoft.Bcl.Async may be an issue. Microsoft.Bcl is installed when one installs Microsoft.Bcl.Async.

Hope this helps others!

like image 57
Rusty Nail Avatar answered Sep 24 '22 19:09

Rusty Nail