KeyVault GetSecretAsync永远不会返回 [英] KeyVault GetSecretAsync never returns

查看:952
本文介绍了KeyVault GetSecretAsync永远不会返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

样品code与KeyVault工作的一个Web应用程序中有如下code它:

The sample code for working with KeyVault inside a web application has the following code in it:

public static async Task<string> GetSecret(string secretId)
{
    var secret = await keyVaultClient.GetSecretAsync(secretId);
    return secret.Value;
}

我已经纳入包括在我的应用程序样本中的 KeyVaultAccessor ​​对象,以测试它。该呼叫将被作为我的网络API控制器的方法之一查询的一部分执行:

I've incorporated the KeyVaultAccessor object included in the sample in my application in order to test it. The call is executed as part of a query to one of my web api controller methods:

var secret = KeyVaultAccessor.GetSecret("https://superSecretUri").Result;

不幸的是,调用不会返回与查询indefintely挂...

Unfortunately, the call never returns and the query hangs indefintely...

可能是什么原因,因为坦率地说我不知道​​从哪里开始...?

What might be the reason, because frankly I have no clue where to start...?

推荐答案

这是我的描述完全在我的博客。总之,异步方法试图返回到等待完成后ASP.NET请求范围内,但要求只允许一个线程的时间,已经有在这方面一个线程(一个阻塞调用结果)。所以任务正在等待的背景是免费的,直到任务完成线程被阻塞的背景:僵局

This is the common deadlock issue that I describe in full on my blog. In short, the async method is attempting to return to the ASP.NET request context after the await completes, but that request only allows one thread at a time, and there is already a thread in that context (the one blocked on the call to Result). So the task is waiting for the context to be free, and the thread is blocking the context until the task completes: deadlock.

正确的解决方案是使用等待而不是结果

The proper solution is to use await instead of Result:

var secret = await KeyVaultAccessor.GetSecret("https://superSecretUri");

这篇关于KeyVault GetSecretAsync永远不会返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆