迭代查询结果时发生异常...取消了任务
An exception occurred while iterating over the results of a query ... A task was canceled
当创建租户时执行我的 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:看了一圈,根据这个post:EF Core: An exception occurred in the database while iterating the results of a query,我认为可能与MARS有关。因此,我已将 ;MultipleActiveResultSets=true
添加到我的连接字符串中。不幸的是,这没有效果。
异常发生前有很长的延迟 - 大约一分钟。
为清楚起见,这里是显示产生异常(断点)的行的屏幕截图
重现的具体步骤:
- 使用 ABP Suite 创建一个新项目
- 在[Project]TenantDatabaseMigrationHandler.cs文件的第98行设置断点(var tenantConfiguration = await _tenantStore.FindAsync(tenantId);).
- 在同一文件的第 126 行设置断点 (_logger.LogException(ex);)
- 调试应用程序,创建新租户。执行应在第 98 行停止。
- 按 F10。执行应在第 126 行停止。该消息应指示超时。
- 修改第 96 行,添加 10 分钟的超时。
- 调试应用程序,创建新租户。执行应在第 98 行停止。
- 按 F10。执行应在第 126 行停止。消息应指示上述异常。
1
正如您从屏幕截图中看到的那样,解决方案是将 requiresNew 参数设置为 false。这是必要的,因为调用方法上已经有一个由 [UnitOfWork] 属性启动的正在处理的事务。
当创建租户时执行我的 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:看了一圈,根据这个post:EF Core: An exception occurred in the database while iterating the results of a query,我认为可能与MARS有关。因此,我已将 ;MultipleActiveResultSets=true
添加到我的连接字符串中。不幸的是,这没有效果。
异常发生前有很长的延迟 - 大约一分钟。
为清楚起见,这里是显示产生异常(断点)的行的屏幕截图
重现的具体步骤:
- 使用 ABP Suite 创建一个新项目
- 在[Project]TenantDatabaseMigrationHandler.cs文件的第98行设置断点(var tenantConfiguration = await _tenantStore.FindAsync(tenantId);).
- 在同一文件的第 126 行设置断点 (_logger.LogException(ex);)
- 调试应用程序,创建新租户。执行应在第 98 行停止。
- 按 F10。执行应在第 126 行停止。该消息应指示超时。
- 修改第 96 行,添加 10 分钟的超时。
- 调试应用程序,创建新租户。执行应在第 98 行停止。
- 按 F10。执行应在第 126 行停止。消息应指示上述异常。
1
正如您从屏幕截图中看到的那样,解决方案是将 requiresNew 参数设置为 false。这是必要的,因为调用方法上已经有一个由 [UnitOfWork] 属性启动的正在处理的事务。