SQL Server An exception occurred while iterating over the results of a query ... A task was canceled

ndasle7k  于 2023-05-05  发布在  其他
关注(0)|答案(1)|浏览(366)

When the following line of code from my ABP generated MyAppTenantDatabaseMigrationHandler is executed upon tenant creation:

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…
      }
    }
  }
}

I am getting the following exception in my log file:

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()

What could be going on? As you can see, I increased the timeout because I was getting Timeout exceptions before this. Thanks.

PS: After looking around, I thought it may be related to MARS based on this post: EF Core: An exception occurred in the database while iterating the results of a query . As a result, I've added ;MultipleActiveResultSets=true to my connection strings. Unfortunately that had no effect.

There is a very long delay before the exception - about a minute.

Just for clarity, here is a screenshot showing the line producing the exception (breakpoint)

Exact steps to reproduce:

  1. Create a new project using ABP Suite
  2. Set a breakpoint on line 98 of the [Project]TenantDatabaseMigrationHandler.cs file (var tenantConfiguration = await _tenantStore.FindAsync(tenantId);).
  3. Set a breakpoint on line 126 of the same file (_logger.LogException(ex);)
  4. Debug the application, creating a new tenant. Execution should stop on line 98.
  5. Press F10. Execution should stop on line 126. The message should indicate a timeout.
  6. Modify line 96, adding a 10 minute timeout.
  7. Debug the application, creating a new tenant. Execution should stop on line 98.
  8. Press F10. Execution should stop on line 126. The message should indicate the exception above.
bhmjp9jg

bhmjp9jg1#

Finally found an answer in this post: https://support.abp.io/QA/Questions/2084/An-exception-occurred-while-iterating-over-the-results-of-a-query-for-context-type-%27VoloSaasEntityFrameworkCoreSaasDbContexspendinng

As you see from the screenshot, the solution was setting the requiresNew arguments to false. This is necessary because there was already a transaction in process started by the [UnitOfWork] attribute on a calling method.

相关问题