0

I faced with deadlock when trying make two parallel calls to DB in action

public async Task<IActionResult> Index(){

 var firstTask = this._uow.GetCountries(); //create task to run it in background

 var secondResult = await _uow.GetStates(); //call method and get result from Task
 var firstResult = await firstTask;  //trying to get result from first task

 return View();
}

And unit of work methods are simple and use Entity framework:

 public async Task<Countries> GetCountries()
        {
           return  await dbContext.Countries.ToListAsync(); // 
        }

 public async Task<States> GetStates()
        {
           return  await dbContext.States.ToListAsync();
        }

Anyway call this methods like this will fix issue and i wonder why?

public async Task<IActionResult> Index(){
   var secondResult = await _uow.GetStates(); 
   var firstResult = await _uow.GetCountries();

 return View();
}

But with this i'm losing main idea - parallel calls

dantey89
  • 2,167
  • 24
  • 37
  • 3
    It's not a deadlock, it's just the problem that EF does not support multiple parallel queries in single instance of DbContext. Take a look at the linked question: https://stackoverflow.com/questions/41749896/ef-6-how-to-correctly-perform-parallel-queries . You should use **separate DbContext instances** to run your queries in parallel. – Marcin Zablocki May 29 '17 at 09:26
  • thanks, you are right, seams this is duplicate. – dantey89 May 29 '17 at 09:36

0 Answers0