【问题标题】:An exception occurred while iterating over the results of a query ... A task was canceled迭代查询结果时发生异常...任务已取消
【发布时间】:2021-12-11 08:40:25
【问题描述】:

当我的 ABP 生成的 MyAppTenantDatabaseMigrationHandler 中的以下代码行在租户创建时执行时:

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, timeout: TimeSpan.FromMinutes(10).TotalMilliseconds.To<Int32>()))
      {
        var tenantConfiguration = await _tenantStore.FindAsync(tenantId); // <-- This line

        //  code continues…
      }
    }
  }
}

我的日志文件中出现以下异常:

2021-10-25 13:45:57.644 -05:00 [INF] Executed DbCommand (93,812ms) [Parameters=[@__ef_filter__p_0='?' (DbType = Boolean), @__id_0='?' (DbType = Guid)], CommandType='"Text"', CommandTimeout='600']
SELECT [t].[Id], [t].[ActivationEndDate], [t].[ActivationState], [t].[ConcurrencyStamp], [t].[CreationTime], [t].[CreatorId], [t].[DeleterId], [t].[DeletionTime], [t].[EditionEndDateUtc], [t].[EditionId], [t].[ExtraProperties], [t].[IsDeleted], [t].[LastModificationTime], [t].[LastModifierId], [t].[Name]
FROM (
    SELECT TOP(1) [s].[Id], [s].[ActivationEndDate], [s].[ActivationState], [s].[ConcurrencyStamp], [s].[CreationTime], [s].[CreatorId], [s].[DeleterId], [s].[DeletionTime], [s].[EditionEndDateUtc], [s].[EditionId], [s].[ExtraProperties], [s].[IsDeleted], [s].[LastModificationTime], [s].[LastModifierId], [s].[Name]
    FROM [SaasTenants] AS [s]
    WHERE ((@__ef_filter__p_0 = CAST(1 AS bit)) OR ([s].[IsDeleted] <> CAST(1 AS bit))) AND ([s].[Id] = @__id_0)
    ORDER BY [s].[Id]
) AS [t]
ORDER BY [t].[Id]
2021-10-25 13:45:57.651 -05:00 [ERR] An exception occurred while iterating over the results of a query for context type 'Volo.Saas.EntityFrameworkCore.SaasDbContext'.
System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.BufferedDataRecord.InitializeAsync(DbDataReader reader, IReadOnlyList`1 columns, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList`1 columns, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader.InitializeAsync(IReadOnlyList`1 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.SplitQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SplitQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()

会发生什么?如您所见,我增加了超时,因为在此之前我收到了超时异常。谢谢。

PS:看了一圈后,根据这个帖子,我认为它可能与火星有关:EF Core: An exception occurred in the database while iterating the results of a query。结果,我将;MultipleActiveResultSets=true 添加到我的连接字符串中。不幸的是,这没有效果。

异常之前有很长的延迟 - 大约一分钟。

为了清楚起见,这里是一个屏幕截图,显示了产生异常(断点)的行

重现的具体步骤:

  1. 使用 ABP Suite 创建新项目
  2. 在 [Project]TenantDatabaseMigrationHandler.cs 文件的第 98 行设置断点 (var tenantConfiguration = await _tenantStore.FindAsync(tenantId);)。
  3. 在同一文件的第 126 行设置断点 (_logger.LogException(ex);)
  4. 调试应用程序,创建新租户。执行应在第 98 行停止。
  5. 按 F10。执行应在第 126 行停止。消息应指示超时。
  6. 修改第 96 行,添加 10 分钟超时。
  7. 调试应用程序,创建新租户。执行应在第 98 行停止。
  8. 按 F10。执行应在第 126 行停止。消息应指出上述异常。

【问题讨论】:

    标签: sql-server entity-framework-core abp


    【解决方案1】:

    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] 属性已经启动了一个正在处理的事务。

    【讨论】:

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