我在 Blazor 页面中添加了一个计时器,因此一些数据(从数据库中提取)可以每 5 秒刷新一次。当我在页面上时它工作正常,但是当我移动到另一个页面并返回到原来的页面时,我收到了这个错误:
“此连接与环境事务一起使用。原始环境事务需要先完成,然后才能在外部使用此连接”我在末尾包含了堆栈跟踪
我试图在页面处理时处理 Timer,但没有解决。看起来这个问题是由于数据库上下文是如何在依赖注入容器中注册的,但是
这是我页面的 C# 代码。它调用主服务,后者又通过存储库获取数据。
@functions {
protected Dictionary<int, int> RunningTaskProcessQueueCountDictionary;
protected Dictionary<int, int> PendingTaskProcessQueueCountDictionary;
protected Timer CountRefreshTimer = null;
protected override async Task OnInitAsync()
{
CountRefreshTimer = new Timer(new TimerCallback(async _ =>
{
await RefreshCount();
await base.Invoke(StateHasChanged);
}), null, 0, 5000);
}
private async Task RefreshCount()
{
RunningTaskProcessQueueCountDictionary = await mainService.GetRunningTaskProcessQueueCountByTaskAppAsync(null);
PendingTaskProcessQueueCountDictionary = await mainService.GetPendingTaskProcessQueueCountByTaskAppAsync(null);
}
public void Dispose()
{
if (CountRefreshTimer != null)
{
CountRefreshTimer.Dispose();
}
}
}
这是注册 DB 上下文的 startup.cs 的摘录:
// Register the DB Context
var connection = Configuration.GetConnectionString("SQL01.xxx");
services.AddDbContext<SQL01xxxContext>(options => options.UseSqlServer(connection));
// Register all repositories and services (using Scrutor)
services.Scan(scan =>
scan.FromAssemblies(typeof(IMainService).Assembly, typeof(ITaskAppRepository).Assembly)
.AddClasses()
.AsMatchingInterface()
.WithScopedLifetime());
services.AddScoped<DbContext, SQL01xxxContext>();
services.AddScoped<IUnitOfWork<SQL01xxxContext>, UnitOfWork<SQL01xxxContext>>();
这是错误的堆栈跟踪
System.InvalidOperationException HResult=0x80131509 Message=此连接用于环境事务。需要先完成原始环境事务,然后才能在外部使用此连接。Source=Microsoft.EntityFrameworkCore.Relational
StackTrace:在 Microsoft.EntityFrameworkCore.Storage.RelationalConnection.HandleAmbientTransactions() 在 Microsoft.EntityFrameworkCore.Storage.RelationalConnection.d__42.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在系统。 Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable
1.AsyncEnumerator.d__12.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__7
2.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task任务)在 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable
1.AsyncEnumerator.d__11.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator
2.d__7.MoveNext() 在 D:\a\1 \s\Ix.NET\Source\System.Interactive.Async\Select.cs:System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
处的第 106 行 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在系统处。 Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在 System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Linq.AsyncEnumerable.AsyncIterator
1.d__10.MoveNext() 在 D:\a\1\s\Ix.NET\Source\System.Interactive.Async\AsyncIterator.cs: 系统中 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 的第 112 行。 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在 System.Runtime.CompilerServices.TaskAwaiter 的 Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)1.GetResult() at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor
1.EnumeratorExceptionInterceptor.d__5.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo System.Linq.AsyncEnumerable.d__6
的 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)的3.MoveNext() in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\Aggregate.cs:line 120 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter
.Throw() SIQC.Enterprise.GenericRepository.Common.IQueryableExtensions.d__1的 GetResult()1.MoveNext() in C:\TFS\[...]\IQueryableExtensions.cs:line 28 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult() 在 SIQC.Enterprise.GenericRepository.RepositoryBase.ReadOnlyRepository1.<GetAsync>d__12.MoveNext() in C:\TFS\[...]\ReadOnlyRepository.cs:line 174 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult() 在 ToolsWebManagementData.IntravexV21.Repository.TaskProcessQueueRepository.d__2.MoveNext() 在 C:\TFS[...]\TaskProcessQueueRepository.cs:line 46 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)的 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at TWMBlazorSSB.Services.MainService.<GetRunningTaskProcessQueueCountByTaskAppAsync>d__21.MoveNext() in C:\TFS\[...]\MainService.cs:line 79 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult() at TWMBlazorSSB.Pages.TaskApp.TaskApps.d__7.MoveNext() in C:\TFS[...]\TaskApps.razor:line 91 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System .Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at TWMBlazorSSB.Pages.TaskApp.TaskApps.<b__6_0> d.MoveNext() 在 C:\TFS[...]\TaskApps.razor:line 79
任何帮助,将不胜感激。谢谢