I have problem with asynchronous controller in ASP.Net MVC 5 application. I'm using Entity Framework 6 Code First approach.
I have a method
public async Task<ActionResult> Index()
{
using(var context = new MyDbContext())
{
var eventsTask = context.Events
.Where(e => e.Enable)
.ToListAsync();
var countTask = context.Users
.CountAsync();
await Task.WhenAll(eventsTask, countTask);
return View(new ViewModel()
{
Events = eventsTask.Result,
Count = countTask.Result
});
}
}
I have two asyncronous methods here. I have measured each of them separately via MiniProfiler. They takes ~85 ms.
But in my method I run them using Task.WhenAll(). I believe it executes Db queries asynchronously and should take about ~85-90 ms for both. But it takes ~170-180. So I have got asynchronous methods run synchronously (following each other).
I think it is because of context. I have a test, when I remove context queries and call many api methods using HttpClient. It takes time equals to longer of them (3 api calling, ~500 ms each of them. Totally method takes ~600 ms). I believe that It is possible to execute EF methods asynchronously.
Does anyone know the solution