C# GraphQL并行异步查询c EF Core 3.0
我正在尝试使用2个或更多异步子查询执行查询。遗憾的是,这是不可能的,因为DbContext一次只能处理1个请求 该项目正在使用: 实体框架3 Graphql 3.0.0 错误 在上一个操作完成之前,在此上下文上启动了第二个操作。这通常是由使用同一DbContext实例的不同线程引起的 类型:C# GraphQL并行异步查询c EF Core 3.0,c#,entity-framework,async-await,graphql,.net-core-3.0,C#,Entity Framework,Async Await,Graphql,.net Core 3.0,我正在尝试使用2个或更多异步子查询执行查询。遗憾的是,这是不可能的,因为DbContext一次只能处理1个请求 该项目正在使用: 实体框架3 Graphql 3.0.0 错误 在上一个操作完成之前,在此上下文上启动了第二个操作。这通常是由使用同一DbContext实例的不同线程引起的 类型: Field<ListGraphType<A>>() .Name("A") .Description("query of A") .ResolveAsync(a
Field<ListGraphType<A>>()
.Name("A")
.Description("query of A")
.ResolveAsync(async context =>
await repro.GetAllAAsync(context)
);
Field<ListGraphType<A>>()
.Name("B")
.Description("query of B")
.ResolveAsync(async context =>
await repro.GetAllBAsync(context)
);
DbContextPool无法解决此问题,我不想使用StructureMap
有优雅的使用方法吗
每个子查询有多个dbContext
等待dbContext空闲
我最近创建了一个解决EF并行执行的项目。 您可以在这里看到实现 特别是,这里有一个查询示例,您可以找到:
public class TestGroupQueries : ObjectGraphType
{
public TestGroupQueries(IHttpContextAccessor _httpContextAccessor)
{
Name = "testQueries";
FieldAsync<TestResponseType>(
"demoQuery",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<IntGraphType>> { Name = "input" }),
resolve: async context =>
{
//extension method that creates scope (IServiceScope) from IServiceProvider
using (var scope = _httpContextAccessor.CreateScope())
{
//call DemoQuery method of TestService; GetService is an extension method that use GetRequiredService from scope serviceprovider
return await scope.GetService<ITestService>().DemoQuery(context.GetArgument<int>("input"));
}
});
}
您在解析器中创建一个新的作用域,并使用该作用域的服务我最近创建了一个解决EF并行执行的项目。 您可以在这里看到实现 特别是,这里有一个查询示例,您可以找到:
public class TestGroupQueries : ObjectGraphType
{
public TestGroupQueries(IHttpContextAccessor _httpContextAccessor)
{
Name = "testQueries";
FieldAsync<TestResponseType>(
"demoQuery",
arguments: new QueryArguments(new QueryArgument<NonNullGraphType<IntGraphType>> { Name = "input" }),
resolve: async context =>
{
//extension method that creates scope (IServiceScope) from IServiceProvider
using (var scope = _httpContextAccessor.CreateScope())
{
//call DemoQuery method of TestService; GetService is an extension method that use GetRequiredService from scope serviceprovider
return await scope.GetService<ITestService>().DemoQuery(context.GetArgument<int>("input"));
}
});
}
您可以在解析器内创建一个新的作用域并使用此作用域的服务虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能无效。-虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则仅链接的答案可能无效-