我正在使用 dotnet 6 和 GraphQL 7。我仅在一个字段中遇到一些问题。仅当应用程序部署在集群(kubernetes)中时才会出现该错误,而不会在本地部署时出现该错误。所以看起来依赖注入没有正常工作,这是查询的代码:
public class UserModulesProgressField : IGraphQueryField
{
public void BindFields(ObjectGraphType parentObject)
{
parentObject.Field<ListGraphType<ModuleProgressType>, IEnumerable<ModuleProgress>>("userModulesProgress")
.Description(GraphQLDescriptionConstants.ProtectedGraphQLDescription)
.Resolve()
.WithScope()
.WithServices<IUnitOfWork, IGraphService>()
.ResolveAsync(async (context, unitOfWork, graphService) =>
{
var user = await graphService.GetCurrentUser();
var userId = user.Id.ToString();
var modulesProgress = (await unitOfWork.ModulesProgressRepository.GetUserModulesProgressAsync(userId)).ToList();
var bank = graphService.GetUserBank(user);
var modules = await unitOfWork.ModuleRepository.GetBankModulesNoIncludeAsync(bank.Id);
return modulesProgress.ToList();
}
);
}
}`
错误位于 GetBankModulesNoIncludeAsync(bank.Id) 内部,其中包含以下内容:
public async Task<IEnumerable<Module>> GetBankModulesNoIncludeAsync(int? bankId)
{
var dbModules = await _context.Modules
.Where(e => e.BankModules.Any(b => b.BankId == bankId))
.OrderBy(e => e.Rank)
.ToListAsync();
dbModules.ForEach(m => m?.OrderByRank());
return dbModules;
}`
我收到的错误是:
[12:37:53 Error] Microsoft.EntityFrameworkCore.Query
An exception occurred while iterating over the results of a query for context type 'DTH.WebApp.Infrastructure.Data.ApplicationDbContext'.
System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'ApplicationDbContext'.
at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.get_StateManager()
at Microsoft.EntityFrameworkCore.Query.QueryContextDependencies.get_StateManager()
at Microsoft.EntityFrameworkCore.Query.QueryContext.InitializeStateManager(Boolean standAlone)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.<>c__DisplayClass30_0`2.<<ExecuteAsync>b__0>d.MoveNext()
我尝试(仅用于调试)添加延迟
await Task.Delay(2000);
就在 var 银行之后,并且延迟正在起作用。但我不喜欢随机延迟,我想了解这里实际发生的情况!这很奇怪,因为它只发生在该查询上,而且它应该可以工作,因为 graphQL 正在处理依赖注入: .Resolve().WithScope().WithServices ...
有什么想法吗?接下来我应该尝试什么?我真的不喜欢放置随机延迟(无论如何,可能少于 2 秒就可以了,还没有尝试,因为每次尝试我都必须重新运行管道)
我尝试更改语法,尝试以另一种方式检索数据库和服务,尝试使用条件,但没有任何效果
看起来只是缺少一个“等待”:
var modulesProgress = (await unitOfWork.ModulesProgressRepository.GetUserModulesProgressAsync(userId)).ToList();
var bank = await graphService.GetUserBank(user); // added await here
var modules = await unitOfWork.ModuleRepository.GetBankModulesNoIncludeAsync(bank.Id);