async all the way down issue
.net, asp.net-mvc, async-await, c#
Solution
Yes. There is a penalty (though not a huge one), and if you don't need to be `async` don't be. This pattern is often called "return await" where you can almost always remove both the `async` and the `await`. Simply return the task you already have that represents the asynchronous operations:
private Task C_Async(int id)
{
// This method executes very fast
var idTemp = paddID(id);
return D_Async(idTemp);
}
private Task D_Async(string id)
{
// This method executes very fast
return E_Async(id);
}
In this specific case `Index` will only `await` the tasks that `E_Async` returns. That means that after all the `I/O` is done the next line of code will directly be `return View();`. `C_Async` and `D_Async` already ran and finished in the synchronous call.
Problem
I have an async asp.net controller. This controller calls an async method. The method that actually performs the async IO work is deep down in my application. The series of methods between the controller and the last method in the chain are all marked with the async modifier. Here is an example of how I have the code setup: ``` public async Task<ActionResult> Index(int[] ids) { List<int> listOfDataPoints = dataPointService(ids); List<Task> dpTaskList = new List<Task>(); foreach (var x in listOfDataPoints) { dpTaskList.Add(C_Async(x)); } await Task.WhenAll(dpTaskList); return View(); } private async Task C_Async(int id) { //this method executes very fast var idTemp = paddID(id); await D_Async(idTemp); } private async Task D_Async(string id) { //this method executes very fast await E_Async(id); } private async Task E_Async(string url) { //this method performs the actual async IO result = await new WebClient().DownloadStringTaskAsync(new Uri(url)) saveContent(result); } ``` As you can see the controller calls C_Async(x) asynchronously then there is a chain of async methods to E_Async. There are methods between the controller and E_Async and all have the async modifier. Is there a performance penalty since there are methods using the async modifyer but not doing any async IO work? Note: This is a simplified version of the real code there are more async methods between the controller and the E_Async method.