Good afternoon.
I looked at many articles on this topic, one still has no complete understanding.
I have an asynchronous method, for example:
public Task<Customer> Get(int id)
{
return await client.GetCustomer(id);
}
I have a synchronous method that should call an asynchronous method, for example:
public Order GetOrder()
{
//Invoking Asynchronous Method and Defining Customer Variable
return new Order { Customer = customer};
}
Question: What is the best way to make this call?
P.S. There are the following ways:
1.First way
var customer = Get(10).Result;
2.Second way
var customer = Get(10).GetAwaiter().GetResult();
3.Third way
var customer = Task.Run(async()=>await Get(10)).Result;
4.Fourth way - Apply the RunSync method of the AsyncHelper class (AsyncHelper from Microsoft)
var customer = AsyncHelper.RunSync(()=>Get(10));
Note - here under the hood creation and execution of Task
5.The fifth way - Apply the Run method of the AsyncContext class library Nito.AsyncEx
var customer = AsyncContext.Run(()=>Get(10));
I would like to get a short and reasoned answer.