在尝试检查异步方法的结果时,我收到以下错误。
我既没有尝试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);
}
Run Code Online (Sandbox Code Playgroud)
您当前正在尝试等待任务的结果:
bool result = await container.GetAwaiter().GetResult().Result;
Run Code Online (Sandbox Code Playgroud)
这是多余的,但也是一个等待发生的死锁问题。(几乎从不.Result
直接调用。)相反,await
产生结果的任务:
bool result = await container.GetAwaiter().GetResult();
Run Code Online (Sandbox Code Playgroud)
编辑:正如在下面的评论中指出的那样,我错过了这container
已经是一项任务。由于整个方法已经是async
,你可以跳过所有的GetAwaiter
东西,直接等待它:
bool result = await container;
Run Code Online (Sandbox Code Playgroud)
编辑:正如评论中进一步指出的那样,您提供的代码似乎与您在屏幕截图中实际使用的代码不匹配。该container
本身不是任务,但它返回你想要的任务的方法:
bool result = await container.ExistsAsync();
Run Code Online (Sandbox Code Playgroud)