0

Summary

I am currently migrating a project to AspNetCore 3.0 and are running into a issue with the GraphQL for .NET ParallelExecutionStrategy when querying for multiple things in one query. The project is using a MSSQL Server as the data store and it is access through Entity Framework Core 3.0. The error I get is:

A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext.

I can resolve the issue if I implement IDocumentExecuter and change the ParallelExecutionStrategy to await each individual execution, from Task.WhenAll to await ExecuteNodeAsync (https://github.com/graphql-dotnet/graphql-dotnet/blob/master/src/GraphQL/Execution/ParallelExecutionStrategy.cs#L27).

Example query that I'm trying to execute:

query {
  thingA {
    id
  }
  thingB {
    id
  }
}

Edit:

Using DbContextPool doesn't seem to solve the problem either:

services.AddDbContextPool<DBCONTEXT>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("CONNECTIONSTRING")));
Ganhammar
  • 1,871
  • 1
  • 16
  • 29

3 Answers3

1

If you are using the built-in dependency injection container, you should consider using an IServiceScopeFactory<T>. It's essentially the same approach as Ganhammar's "StructureMap" based answer, except it's not a "Service Locator". The (relatively simple) IServiceScopeFactory<T> code is here and another answer related to this question is here.

Granger
  • 3,639
  • 4
  • 36
  • 34
0

We are using StructureMap for dependency injection, which is creating a new DbContext per HttpRequest, but we needed a unique DbContext for each query, the solution was to create a nested container and request the dependencies through the nested container.

public Constructor(IContainer container) => _dbContext = container.GetNestedContainer()
    .GetInstance<DbContext>();
Ganhammar
  • 1,871
  • 1
  • 16
  • 29
0

See my implementation that solves this proboem. The "trick" is create scope at resolver level https://github.com/fenomeno83/graphql-dotnet-globalization-demo