I call the asynchronous library method with ConfigureAwait (false). But I'm still at a standstill. (I use it in the ASP.NET API) But, if I use the same method enclosed in Task.Run (), it works fine.
My understanding: if the library method does not use ConfigureAwait inside, then adding ConfigureAwait will not solve the problem, since in the library call this will lead to a deadlock (we block it with .Result). But, if so, why does it work in Task.Run (), since it will not be able to continue in the same context / thread.
This is stated in the article . By the way, I have many articles by Stephen Cleary. But why Task.Run () works in secret.
Code snippet:
// This Create Method results in Deadlock public async Task<string> Create(MyConfig config) { Document doc = await Client.CreateDocumentAsync(CollectionUri, config).ConfigureAwait(false); return doc.Id; } // Uses Task.Run() which works properly, why?? public string Create(MyConfig config) { Document doc = Task.Run(() => Client.CreateDocumentAsync(CollectionUri, config)).Result; return doc.Id; } [HttpPost] public ActionResult CreateConfig(MyConfig config) { string id = Create(config).Result; return Json(id); }
c # async-await
Krunal Modi
source share