0

I need to use the KeyVaultClient in a synchronous method. I have so far tried:

var result = Task.Run(async () => await kvClient.GetSecretAsync(keyUrl)).Result;

and

var result = Task.Run(() => kvClient.GetSecretAsync(keyUrl)).Result;

and

var result = kvClient.GetSecretAsync(keyUrl).Result;

but all of them hang and never return anything. I have also tried:

var secretTask = kvClient.GetSecretAsync(keyUrl);
secretTask.Start();
Task.WaitAll(secretTask);
var result = secretTask.Result;

but that gave me an error message on "Start()": Start may not be called on a promise-style task.

So then I tried:

var task = kvClient.GetSecretAsync(keyUrl);
task.RunSynchronously();
var result = task.Result;

Which threw the error: RunSynchronously may not be called on a task not bound to a delegate, such as the task returned from an asynchronous method.

What should I do? Similar questions on StackOverflow only led me to the above answers.

i .
  • 485
  • 2
  • 10
  • 23
  • Well, you should call asynchronous functions in an asynchronous way. You have to use `await` – Camilo Terevinto Jun 05 '19 at 18:27
  • I can't. Unfortunately if I made this function async, and the function that called this function async, and followed the chain up, it ends at a constructor. I cannot use await here. – i . Jun 05 '19 at 18:30
  • Well, that's the problem. Constructors cannot go launching background work nobody asked for. Use a static (called "factory") asynchronous method that gets the data and returns the instantiated object – Camilo Terevinto Jun 05 '19 at 18:32

0 Answers0