I'm using a couple of dataloaders that use injected query services (which in turn have dependencies on a DbContext). It looks something like this:
Field<ListGraphType<UserType>>(
"Users",
resolve: context =>
{
var loader = accessor.Context.GetOrAddBatchLoader<Guid, IEnumerable<User>>(
"MyUserLoader",
userQueryService.MyUserFunc);
return loader.LoadAsync(context.Source.UserId);
});
Field<ListGraphType<GroupType>>(
"Groups",
resolve: context =>
{
var loader = accessor.Context.GetOrAddBatchLoader<Guid, IEnumerable<Group>>(
"MyGroupLoader",
groupQueryService.MyGroupFunc);
return loader.LoadAsync(context.Source.GroupId);
});
When I run a nested query that concurrently uses both dataloaders I get an exception "A second operation started on this context before a previous asynchronous operation completed"
because both dataloaders are using the same DbContext at the same time.
What's the best way to allow concurrent database access within the query without having to carefully manage DbContexts with ServiceLifeTime.Transient
? Or can dataloader expose a way to know when to dispose of transient DbContexts?
The problem will not be solved with switching from "Scoped" to "Transient" because Gql.Net field resolvers execute in parallel.
Based on your example, I'm expecting that your DbContext
is constructor-injected into your "db service" classes (userQueryService
and groupQueryService
), and those were constructor-injected into your example GraphType class. So each of your db services have the exact same, scoped copy of your DbContext
.
The solution is to lazy-resolve your DbContext
.
You'd change your db-services to inject an IServiceScopeFactory
. You then use that in your loader methods (MyUserFunc
and MyGroupFunc
) to create a scope and then resolve your DbContext
. The problem with this approach ("Service Locator") is that the dependency on your DbContext
is hidden inside of your class.
Use this relatively simple bit of code here on CodeReview.StackExchange to instead use an IServiceScopeFactory<T>
. You get the lazy-resolving without doing "Service Locator"; your strongly-typed dependencies are declared up in the constructor.
So pretend your userQueryService
variable's class is like so:
MyDbContext _dbContext;
public UserQueryService(MyDbContext dbContext) => _dbContext = dbContext;
public async Task<IDictionary<Guid, IEnumerable<User>> MyUserFunc(IEnumerable<Guid> userIds)
{
// code that uses _dbContext and returns the data...
}
Change it to this (again, using the IServiceScopeFactory<T>
):
IServiceScopeFactory<MyDbContext> _dbFactory;
public UserQueryService(IServiceScopeFactory<MyDbContext> dbFactory) => _dbFactory = dbFactory;
public async Task<IDictionary<Guid, IEnumerable<User>> MyUserFunc(IEnumerable<Guid> userIds)
{
using var scope = _dbFactory.CreateScope();
var dbContext = scope.GetRequiredService();
// code that uses dbContext and returns the data...
}
Now, when Gql.Net's resolvers (well, in this case, the data loader) ultimately execute this method, each use of your DbContext
use their own scope and so they won't have execution problems like you have now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With