1

I have the problem when i do 1 fetch it works but when i try to fetch 4 calls att the same time 1 succedes and the other 3 fails.

    public IHttpActionResult GetAll() {
        var products = _context.Products
            .ToList();

        if (products == null)
            return NotFound();

        return Ok(products.Select(x => CreateDto(x)));
    }

This is the error

An error has occurred.","ExceptionMessage":"There is already 
an open DataReader associated with this Command which must be 
closed first.

When i google this they suggest i add to the connectionsstring

MultipleActiveResultSets=True

Then i get another error

ExecuteReader requires an open and available Connection. 
The connection's current state is open.

The suggested by google is then. That i use the dbcontext wrong ExecuteReader requires an open and available Connection. The connection's current state is Connecting

My dbcontext looks like. So im not doing any funky with that.

public class PContext : DbContext {

    public IDbSet<Products> Products{ get; set; }

}

When im return Ok(products.Select(x => CreateDto(x))); in the first line of code i do a lot of selects and things like that. If i remove some heavy code here it works. But it feels like the problem is that this stuff cant handle multiple calls. I mean it should depend on the size of the job it should return when its finished.

Any ideas?

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62
heathrow
  • 93
  • 8
  • Probably you have a single dbcontext used in multiple requests. Make sure you create one per request. – Peter Bons Jun 03 '19 at 14:43
  • I have a basecontroller that runs protected readonly PContext _context; public ApiControllerBase(PContext context) { _context = context; } – heathrow Jun 03 '19 at 14:53
  • And when is that context created, the one that is passed to the base controller? – Peter Bons Jun 03 '19 at 15:12
  • Im not sure from the controller i call public ProductsController(PdbContext context) : base(context) { } This is confusing. Its not invoking new – heathrow Jun 04 '19 at 05:47
  • I tried calling new to the base the result is the same. Its not working – heathrow Jun 04 '19 at 06:00

1 Answers1

0

From the top of my head try this

public IHttpActionResult GetAll() {
    var products = _context.Products;

    if (products == null)
        return NotFound();
    var convertToListFirst = products.Select(x => CreateDto(x)).ToList();
    return Ok(convertToListFirst);
}

I remember converting toList at the wrong time in the past.

Andy
  • 2,124
  • 1
  • 26
  • 29
  • Thanks for the input. I have already tried this. Then i get the same error: ExceptionMessage=ExecuteReader requires an open and available Connection. The connection's current state is open. When having the MultipleActiveResultSets=True set. And without the first error: ExceptionMessage=There is already an open DataReader associated with this Command which must be closed first. – heathrow Jun 03 '19 at 14:46