This is somewhat related to Why use Async/await all the way down . I have some legacy EF Core I need to maintain with lots of nested calls that are not async. I am wondering if it is just ok to make the top level controller only async and wrap the service call with a Task.Run().
What is the difference between these 2 controller actions. In the first one, EF core is called synchronously. But the call is wrapped in a Task.Run so I assume it will be executed asynchronously
In the second, the EF core calls is async But is it even necessary since the controller action is async? Will async within async within async... give any performance boost over just one async call at the top?
case 1
//controller, async call
public async Task<ActionList<string>> GetNames()
{
var names = await Task.Run(() => peopleService.GetNames());
return Ok(names);
}
//people service layer, no async
public List<string> GetNames()
{
return _context.People.Where(...).Select(x => x.Name).ToList();
}
case 2
//controller, async
public async Task<ActionList<string>> GetNames()
{
var names = await peopleService.GetNames();
return Ok(names);
}
//people service layer, async
public async Task<List<string>> GetNamesAsync()
{
return await _context.People.Where(...).Select(x => x.Name).ToListAsync();
}