Given the following code snippet of an aspnet
controller, is it a requirement to have async
/await
at the top level, or is it valid to return a Task
?
The scenario assumes there is some IO (data access) that uses async in underlying code in the repository.
controller
public class SomeController
{
private readonly ISomeService service;
public SomeController(ISomeService service) => _service = service;
public async Task<SomeResult> GetAsync()
{
return await _someService.GetAsync();
}
}
service
public class SomeService
{
private readonly ISomeRepo repo;
public SomeController(ISomeRepo repo) => _repo = repo;
public async Task<SomeResult> GetAsync()
{
return await _repo.FirstOrDefault();
}
}
Thus in the above, is the async
/await
required? It compiles fine without. Async Fixer suggests async await is not required either.
This is a great article:
But this is contradictory to other bits of information.
When should I use Async Controllers in ASP.NET MVC?
https://exceptionnotfound.net/using-async-and-await-in-asp-net-what-do-these-keywords-mean/
async/await in MVC controller's action
But the question is, what is the correct implementation for async
/await
in the above aspnet context?
Feel free to list various caveats.