0

I am using MVC5 + Entity Framework and I am getting an strange exception.The exception seems to be due to a concurrency problem when multiple users are using the site at the same time. The exception says:

System.NotSupportedExceptionA second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.

Now based on the logs, the culprit is the following method:

public async Task<Customer> GetUserByUserIdAsync(string userId)
{
    return await DbContext.Users.FirstOrDefaultAsync(u => u.Id == userId);
}

I can't see what is causing it. Looking at the error, which says use await...as you can see I have await behind it.

Anyone can help please?

Thank you.

Edit: My scenario is using Unit of Work and I have got several layers between controller and the depth of my data layer.

  • What's the life cycle of `DbContext`? And what happens in the method calling `GetUserByUserIdAsync` (if any)? – Gert Arnold Jul 28 '18 at 11:05
  • This method is the one throwing the exception. That means another one is not properly being awaiten before this method is called. Also, keep in mind the DbContext lifetime should be relatively short (this can also be related to this error). – DevilSuichiro Jul 28 '18 at 11:13
  • @GertArnold thank you for your reply. I am using Autofac and the lifetime is set as `AsImplementedInterfaces().InstancePerLifetimeScope()`. Now the method that calls it is being called by a few above it...all using await. The top function is doing `ViewBag.UserDetails= await _userService.UserDetails();` –  Jul 28 '18 at 11:16
  • @DevilSuichiro thanks for the reply. Possibly. I might remove the async as marcus.braun has suggested and see what happens. –  Jul 28 '18 at 11:20
  • 3
    I don't know AutoFac, but I think this scope causes the trouble. Your context is clearly used in multiple parallel requests. Try `InstancePerRequest`. – Gert Arnold Jul 28 '18 at 11:20
  • 1
    Possible duplicate of [EF 6 - How to correctly perform parallel queries](https://stackoverflow.com/questions/41749896/ef-6-how-to-correctly-perform-parallel-queries) – Peter Bons Jul 28 '18 at 12:24
  • If you can [edit] your question and include the entire call chain (from the action in the controller to the posted code), it's going to be easier for us to help you. – Camilo Terevinto Jul 28 '18 at 12:36
  • Thank you @CamiloTerevinto. The nightmare is over and it is fixed now. –  Jul 29 '18 at 01:34
  • Thank you so so much everyone. Finally it is fixed. @GertArnold you were right. Using `InstancePerRequest` fixed it.I am using Unit of Work in my DB and have got several layers from the controller to the lowest level DBContext. Now I initially did use `InstancePerRequest` but then introduced Quertz scheduler to do DB maintenance. However, Quartz access DB not through requests but through clock ticks. So after checking in Autofac doco. I believed,it is ok to change to `InstancePerLifetimeScope`. I was wrong. :) @GertArnold I don't know how I can mark your reply as answer. Please post as answer. –  Jul 29 '18 at 01:38

0 Answers0