I am using a library which provides methods ending with ...Async
and return Task
. I am going to use these in a command line application. So I need to call them synchronously a lot.
C# of course does not allow calling these methods in Main
method since you cannot use async
modifier on Main
method. Assume this is the task:
var task = datastore.Save(data);
I found several solutions like:
Tasks.WaitAll(task);
task.Wait();
however all these wrap thrown exceptions in AggregateException
, I don't want that. I just want to say task.Result
and I expect the original exception to be thrown.
When I use a method returning Task<TResult>
, task.Result
throws AggregateException
even though there are no continuation tasks set. Why is this hapening?
I also have tried,
task.RunSynchronously();
it gives error:
RunSynchronously may not be called on a task not bound to a delegate, such as the task returned from an asynchronous method.
so I guess that's not for methods marked as async
.
Any ideas on patterns using libraries designed for async apps in console apps where there is no asynchronous context?