5

I have to call some async methods in the Application_PostAcquireRequestState method of my Global.asax (these methods comes from a library and there's no equivalent sync method for these operations). I want to be sure this async code complete before continuing with my page process because some security parameters are set with the result of that async call.

What will be the proper way to make this work without creating deadlocks ?

Thanks

mberube.Net
  • 2,120
  • 2
  • 29
  • 39
  • will this help? http://stackoverflow.com/questions/5095183/how-would-i-run-an-async-taskt-method-synchronously – naveen Apr 28 '14 at 15:52

1 Answers1

9

Just call the Result property of the Task that the *Async() methods return, for example.

var result = BarAsync().Result;

If the methods return Task rather than Task<T>, use Wait():

BarAsync().Wait();
Martin Costello
  • 9,672
  • 5
  • 60
  • 72
  • It works but I'm a bit confused : why does it works in global.asax and not in the code behind of a page (deadlock) ? – mberube.Net Apr 28 '14 at 17:51
  • @mberube.Net This question may enlighten you: http://stackoverflow.com/questions/12304691/why-are-iis-threads-so-precious-as-compared-to-regular-clr-threads – Martin Costello Apr 28 '14 at 17:54
  • FYI: I should mention that this is not working inside the constructor! it can be so buggy! Just as a fact. – OmG Dec 14 '16 at 15:28