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