【问题标题】:An exception occurred while iterating over the results of a query for context type 'Volo.Saas.EntityFrameworkCore.SaasDbContext迭代上下文类型“Volo.Saas.EntityFrameworkCore.SaasDbContext”的查询结果时发生异常
【发布时间】:2021-11-04 03:10:21
【问题描述】:
  • ABP 框架版本: v4.4.3
  • 界面类型:MVC
  • 数据库提供程序:EF Core
  • 分层 (MVC) 或身份服务器分离 (Angular):是

异常消息和堆栈跟踪

2021-11-03 21:21:43.774 -05:00 [ERR] 迭代上下文类型“Volo.Saas.EntityFrameworkCore.SaasDbContext”的查询结果时发生异常。 System.Threading.Tasks.TaskCanceledException:任务被取消。 在 Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.BufferedDataRecord.InitializeAsync(DbDataReader 阅读器,IReadOnlyList1 columns, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 列,CancellationToken cancelToken) 在 Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 columns, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.InitializeReaderAsync(DbContext _,布尔结果,CancellationToken cancelToken) 在 Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState 状态,Func4 operation, Func4 verifySucceeded,CancellationToken cancelToken) 在 Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.MoveNextAsync() System.Threading.Tasks.TaskCanceledException: A task was canceled. at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.BufferedDataRecord.InitializeAsync(DbDataReader reader, IReadOnlyList1 列,CancellationToken cancelToken) 在 Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 columns, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 列,CancellationToken cancelToken) 在 Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject 参数对象,CancellationToken 取消令牌) 在 Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject 参数对象,CancellationToken 取消令牌) 在 Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func4 操作,Func4 verifySucceeded, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.MoveNextAsync() 2021-11-03 21:21:43.838 -05:00 [ERR] 任务已取消。 System.Threading.Tasks.TaskCanceledException:任务被取消。 在 Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.BufferedDataRecord.InitializeAsync(DbDataReader 阅读器,IReadOnlyList1 columns, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 列,CancellationToken cancelToken) 在 Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList1 columns, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.InitializeReaderAsync(DbContext _,布尔结果,CancellationToken cancelToken) 在 Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState 状态,Func4 operation, Func4 verifySucceeded,CancellationToken cancelToken) 在 Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable1.AsyncEnumerator.MoveNextAsync() at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable1 asyncEnumerable, CancellationToken cancelToken) 在 Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable1 asyncEnumerable, CancellationToken cancellationToken) at Volo.Abp.Domain.Repositories.EntityFrameworkCore.EfCoreRepository3.FindAsync(TKey id, Boolean includeDetails, CancellationToken cancelToken) 在 Castle.DynamicProxy.AsyncInterceptorBase.ProceedAsynchronous[TResult](IInvocation 调用,IInvocationProceedInfo 继续信息) 在 Volo.Abp.Castle.DynamicProxy.CastleAbpMethodInvocationAdapterWithReturnValue1.ProceedAsync() at Volo.Abp.Uow.UnitOfWorkInterceptor.InterceptAsync(IAbpMethodInvocation invocation) at Volo.Abp.Castle.DynamicProxy.CastleAsyncAbpInterceptorAdapter1.InterceptAsync[TResult](IInvocation 调用,IInvocationProceedInfo 继续信息,Func3 proceed) at Volo.Saas.Tenants.TenantStore.GetCacheItemAsync(Nullable1 id,字符串名称) 在 Volo.Saas.Tenants.TenantStore.FindAsync(Guid id) 在 D:\Century\Internal\Clients.Link\Clients.Link\src\ClientsLink.Domain\Data\ClientsLinkTenantDatabaseMigrationHandler.cs:line 98 中的 ClientsLink.Data.ClientsLinkTenantDatabaseMigrationHandler.MigrateAndSeedForTenantAsync(Guid tenantId, String adminEmail, String adminPassword)

这发生在下面突出显示的代码行上:

private async Task MigrateAndSeedForTenantAsync(
    Guid tenantId,
    string adminEmail,
    string adminPassword)
{
  try
  {
    using (_currentTenant.Change(tenantId))
    {
      // Create database tables if needed
      using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
      {
        var tenantConfiguration = await _tenantStore.FindAsync(tenantId); // EXCEPTION THROWN HERE
        if (tenantConfiguration?.ConnectionStrings != null &&
            !tenantConfiguration.ConnectionStrings.Default.IsNullOrWhiteSpace())
        {
          foreach (var migrator in _dbSchemaMigrators)
          {
            _logger.LogDebug($"Running migration {migrator.ToString()}");
            await migrator.MigrateAsync();
          }
        }

        await uow.CompleteAsync();
      }

重现问题的步骤

  1. 使用 ABP Commercial Suite v 4.4.3 生成示例分层 MVC 应用程序
  2. 向全局实体添加几个实体 - 一个全局实体和一个具有导航属性的多租户
  3. 创建示例数据库
  4. 运行 DbMigrator - 主机数据库将成功迁移
  5. 运行 Web 应用程序,以管理员身份登录,创建新的测试租户 - 大约一分钟后,UI 报告内部错误并记录上述异常

在工作了两个星期之后似乎相当明显,有一些东西阻止了 _tenantStore.FindAsync(tenantId) 调用的执行。

哪些可能的事情会导致调用超时或被取消?

请考虑您的回答并尽可能详细。

我已经换掉了每个组件。我在两台不同的机器上构建和重建。我重写了我的数据种子贡献者。我已将 System.Data.SqlClient 换成 Microsoft.Data.SqlClient 并将命令超时连接字符串参数设置为两个 3600。我在两周内尝试过的任何方法都无法解决此问题。

【问题讨论】:

标签: entity-framework-core abp


【解决方案1】:

终于在这篇帖子里找到了答案:https://support.abp.io/QA/Questions/2084/An-exception-occurred-while-iterating-over-the-results-of-a-query-for-context-type-%27VoloSaasEntityFrameworkCoreSaasDbContexspendinng

从屏幕截图中可以看出,解决方案是将 requiresNew 参数设置为 false。这是必要的,因为调用方法上的[UnitOfWork] 属性已经启动了一个正在处理的事务。

【讨论】:

    猜你喜欢
    • 2023-02-02
    • 2021-04-02
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    相关资源
    最近更新 更多