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?