Visual Studio complains on the following:
public RelayCommand SendRegistrationCommand { get; private set; }
public async void SendRegistration()
{
HttpClient client = new HttpClient();
var response = await client.GetStringAsync("url");
// ... todo
}
Cannot await 'System.Threading.Tasks.Task<string>'
I thought I've done this before, GetStringAsync is not awaitable?
If you don't await the task or explicitly check for exceptions, the exception is lost. If you await the task, its exception is rethrown. As a best practice, you should always await the call.
async and awaitInside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.
The await() method of the Java CyclicBarrier class is used to make the current thread waiting until all the parties have invoked await() method on this barrier or the specified waiting time elapses.
For methods other than event handlers that don't return a value, you should return a Task instead, because an async method that returns void can't be awaited. Any caller of such a method must continue to completion without waiting for the called async method to finish.
You're probably targeting .NET 4.0.
If you can, switch to .NET 4.5 which supports async/await semantics.
If you can't, consider using the Async Targeting Pack: https://www.nuget.org/packages/Microsoft.CompilerServices.AsyncTargetingPack
Edit
As per the comments, Microsoft.Bcl.Async
should be used instead: https://www.nuget.org/packages/Microsoft.Bcl.Async
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With