0

I noticed this exception being thrown in some code someone else wrote and am trying to fix it. We are using ASP.NET MVC with Entity Framework 6.0. I've looked elsewhere but not finding anything that's relevant because the exception in my case is being created by a race condition (rather than an object which isn't cleaned up).

Background:

  • A js function is iterating through an array and making an ajax call to a controller for each item.
  • The controller calls a service, and ultimately the repository which is using entity framework + linq, which looks something like this:

    public IList<int> GetData(IList<int> ids)
    {
        var query = from widget in _context.Widgets
                        .Where(x => x.SourceId == 1 &&
                            ids.Contains(x.ID))
                    from widgetSourceType in widget.WidgetSource
                        .Where(x => x.WidgetSourceType.Name == "foo")
                    select widget.ID;
    
        return query.ToList();
    }
    

It's bombing out on the query.ToList() line, which I believe is when the query is actually executed. It looks pretty clear to me that the ajax calls are firing multiple operations concurrently, and the second is trying to access the same connection before the first is done with it. But I'm not sure on how to fix. Has anyone had experience with this?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Switch386
  • 454
  • 6
  • 19
  • You will have to provide the relevant code around `_context`. I assume that is the `DbContext` instance. Where and when is it created and Where and when is it destroyed. – Igor Mar 09 '18 at 21:53
  • 1
    Wild guess: `_context` is a Singleton `DbContext` instance. You will need to provide the entire stack trace though (from where that `_context` comes from, how you get to the posted code, etc) – Camilo Terevinto Mar 09 '18 at 21:53
  • Where are u declaring the _context? – Hussein Salman Mar 09 '18 at 21:58
  • Short answer is...I have to do some research to make sure I don't break something else. Longer answer: We are using Castle Windsor to inject the repository into the service and the service into the controller. The context lives in the object and is scoped to the class level. I'm wondering if it is one instance of the class being shared around. Not sure how those are being instantiated without digging in. However, I think I could take the suggestion from below and convert it to a using block. – Switch386 Mar 09 '18 at 23:02
  • 1
    See related [Castle Windsor ApiController Factory implementation for ASP.NET Web API](https://stackoverflow.com/q/9557909/1260204) – Igor Mar 10 '18 at 07:02

1 Answers1

1

I assume the issue is in _Context. Try to use using statement in order to clear any opened connections.

using (_context conn =
    new YourDBContext('connectionString'))
{
    conn.Open();
    //your query
    conn.Close();
}

Or you can use async queries and then use await to get the result.

Hussein Salman
  • 7,806
  • 15
  • 60
  • 98