The Inquisitive Coder
The Inquisitive Coder

Reputation: 1315

'bool' does not contain a definition for 'GetAwaiter'

While trying to check the result of an asynchronous method I'm getting the below error.

Neither I tried await container.ExistsAsync().Result nor bool result = await container.GetAwaiter().GetResult(); worked.

Where am I going wrong?

[TestMethod]
public async Task StorageAccountConnectionTest()
{
    var storageCredentials = new StorageCredentials(_mockFuncXTransConfiguration.Object.StorageAccountName, _mockFuncXransConfiguration.Object.StorageAccountKey);
    var cloudStorageAccount = new CloudStorageAccount(storageCredentials, true);
    var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
    var container = cloudBlobClient.GetContainerReference(_mockFuncXTransConfiguration.Object.BlobName);
    bool result = await container.ExistsAsync().Result;
    Assert.AreEqual(true, result);
}

enter image description here

Upvotes: 0

Views: 10712

Answers (1)

David
David

Reputation: 219097

You're currently trying to wait the result of the task:

bool result = await container.GetAwaiter().GetResult().Result;

Which is redundant, but also a deadlock problem waiting to happen. (Almost never call .Result directly.) Instead, await the task which produces the result:

bool result = await container.GetAwaiter().GetResult();

Edit: As pointed out in the comments below, I'd missed that the container is already a task. Since the whole method is already async, you can skip all of the GetAwaiter stuff and just await it directly:

bool result = await container;

Edit: As further pointed out in comments, it looks like the code you provided doesn't match the code you're actually using in the screen shot. The container itself isn't the task, but has a method which returns the task you want:

bool result = await container.ExistsAsync();

Upvotes: 7

Related Questions