Reputation: 1315
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);
}
Upvotes: 0
Views: 10712
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