KeyVault GetSecretAsync never returns

前端 未结 5 1456
醉话见心
醉话见心 2021-01-03 10:37

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

public static async Task GetSecret(string secr         


        
5条回答
  •  渐次进展
    2021-01-03 10:52

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

    You have a classic deadlock. That's why you shouldn't block on async code. Behind the scenes, the compiler generates a state-machine and captures something called a SynchronizationContext. When you synchronously block the calling thread, the attempt to post the continuation back onto that same context causes the deadlock.

    Instead of synchronously blocking with .Result, make your controller async and await on the Task returned from GetSecret:

    public async IHttpActionResult FooAsync()
    {
        var secret = await KeyVaultAccessor.GetSecretAsync("https://superSecretUri");
        return Ok();
    }
    

    Side note - Async methods should follow naming conventions and be postfixed with Async.

提交回复
热议问题