Entity framework: Different connection issues occurring occasionally

asp.net-mvc c# entity-framework entity-framework-6

Accepted Answer

I was able to identify the source of my problem with the help of @Gerd Arnold and @Chris Pratt's suggestions.

In fact, the exceptions were brought on by a DbContext object that was utilised simultaneously across many queries. This DbContext is a component of a service that an action filter that uses Unity property injection has access to. What I didn't realise at the time is that action filters in APS.NET MVC are cached and reused rather than being created for each request. Therefore, avoid injecting instances of DbContext or classes based on DbContext into action filters!

That problem was resolved by contactingDependencyResolver.Current.GetService<ClassType>() rather than utilisingDependency -attribute in the code of our filter to get an instance of the appropriate dependency (Please note that you lose the testability of the filter through this workaround)

Finding the answer took me a long time. I would advise users who sometimes see the same issues as those in my query to look for any DbContext-based classes that could be utilised simultaneously across requests or threads in your application.

  1. Verify the Unity IoC guidelines.
  2. Look for classes that may be used simultaneously but are not instantiated by IoC.
  3. Verify each step along the Request lifecycle in MVC path.
3/15/2017 7:59:58 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow