Consider following async MVC controller method (There are ~100.000 Customers in database):
public async Task<IEnumerable<Customer>> GetCustomerss()
{
var query = dbContext.Set<Customer>();
Debug.WriteLine("Getting customers");
var task = query.ToListAsync();
Debug.WriteLine("After getting customers task");
var taskRes = await task;
Debug.WriteLine("After awaiting customers");
var query2 = dbContext.Set<Customer>();
Debug.WriteLine("Getting customers 2");
var task2 = query2.ToListAsync();
Debug.WriteLine("After getting customers task 2");
var taskRes2 = await task2;
Debug.WriteLine("After awaiting customers 2");
return taskRes2;
}
The problem is that in both blocks (query and query2) thread hangs in line with ToListAsync() for about 10 seconds and after that (when printing "After getting customers task..." the task status is RanToCompletion. It seems that the await happens not where await keyword is.
I am aware that EF6 context is not thread safe, this is just an example code. The db is SQL Server 2014 hosted on Azure. ToListAsync() is extension method from System.Data.Entity;
UPDATE: It seems that I missed one important thing - the app uses MiniProfiler and it's probable source of a the problem as suggested in: How to make Entity Framework execute asynchronously