I'm attempting to correctly convert a synchronous controller method to asynchronous, given that the operation it performs is CPU-intensive.
private MyDbContext db = new MyDbContext();
public async Task<ActionResult> Index()
{
List<DashboardItemViewModel> viewModels = new List<DashboardItemViewModel>();
List<ProductModel> products = db.Products.ToList();
await Task.Run(() =>
{
foreach (ClientModel client in db.Clients)
{
// Constructor uses lots of logic, mostly looping through the list
// many times
viewModels.Add(new DashboardItemViewModel(client, products));
}
});
return View(viewModels);
}