[英]'bool' does not contain a definition for 'GetAwaiter'
在嘗試檢查異步方法的結果時,我收到以下錯誤。
我既沒有嘗試過await container.ExistsAsync().Result
也沒有嘗試bool result = await container.GetAwaiter().GetResult();
工作。
我哪里錯了?
[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);
}
您當前正在嘗試等待任務的結果:
bool result = await container.GetAwaiter().GetResult().Result;
這是多余的,但也是一個等待發生的死鎖問題。 (幾乎從不直接調用.Result
。)相反, await
產生結果的任務:
bool result = await container.GetAwaiter().GetResult();
編輯:正如在下面的評論中指出的,我錯過了container
已經是一項任務。 由於整個方法已經是async
,您可以跳過所有GetAwaiter
內容並直接等待它:
bool result = await container;
編輯:正如評論中進一步指出的那樣,您提供的代碼似乎與您在屏幕截圖中實際使用的代碼不匹配。 container
本身不是任務,但有一個方法可以返回您想要的任務:
bool result = await container.ExistsAsync();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.