What is the preferred way to run an async method synchronously?
This is what I currently do:
void RunSync() {
var task = Task.Run(() => RunAsync());
task.Wait();
}
async Task RunAsync() {
throw Exception("test");
}
My problem with the above code is that task.Wait()
throws an AggregateException
rather than the actual exception thrown by RunAsync
which is what I'd prefer.
Is there a way to propagate the actual exception from RunAsync
without resorting to catching AggregateException
and unwrapping?