Tuesday, March 12, 2019

Executing an async method synchronously in C#

void NotAnAsyncMethod()
{
try
{
   var answer = someAsyncMethod().GetAwaiter().GetResult();
}
catch (Exception ex)
{
   // ex is our application's exception instead of aggregate exception
   //    aggregate exception is thrown if .Wait() and .Result are used instead
}
}