OnAfterRenderAsync我的blazor 頁面中有以下功能:
protected override async Task OnAfterRenderAsync(bool firstRender)
{
try
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("initializeDropZone");
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
var userSecurityInfo = await UserManager.GetUserAsync(user);
var userDbImages = (await ImageData.GetImagesByUser(userSecurityInfo.Id)).ToList();
foreach (var dbImage in userDbImages)
{
_userFiles.Add(new ImageModel()
{
ID = dbImage.ID,
DatasetName = dbImage.DatasetName,
Name = dbImage.FileName,
UploadDate = dbImage.UploadDate,
BucketUrl = dbImage.BucketUrl,
BucketUrlExpireDate = dbImage.BuckeUrlExpireDate
});
}
}
StateHasChanged();
}
}
catch(Exception e)
{
Console.WriteLine("OnAfterRenderError (DataManagement):" e.Message);
}
await base.OnAfterRenderAsync(firstRender);
return;
}
雖然通常此操作在服務器第一次啟動后非常快(它只加載影像 URL 而不是實際影像資料),但需要一點時間。如果用戶決定在操作仍在運行時更改頁面,我會收到以下錯誤:
fail: Microsoft.EntityFrameworkCore.Query[10100] An exception occurred while iterating over the results of a query for context type 'WebApplication.Data.ApplicationDbContext'. System.InvalidOperationException: A second operation was started on this context before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913. at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection() at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable
1.AsyncEnumerator.MoveNextAsync() System.InvalidOperationException: A second operation was started on this context before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913. at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection() at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.AsyncEnumerator.MoveNextAsync()
- The second page in this case also has some similar db operation
- The actual data accession is not done in the EF database ASP.NET creates automatically, I store my actual data in a separate MS SQL database, which is accessed using dapper.
- There are some similar posts on SO, such as this one. The answer suggest to use a *
DbContextFactory. However, I have tried the suggested code, but with that my application crashes during startup with aSystem.AggregateException. I don't fully understand why. - I don't quite understand, why there is a problem with multiple EF accesses, as I only access the security DB very shortly to get the user ID. This should in theory be done long before the user has the chance to switch pages.
Any suggestions on how to fix this and understand the issue better are appreciated. So far I simply catch the exception, so the application doesn't crash, but I would like to prevent it in the first place.
uj5u.com熱心網友回復:
我的猜測是這一行:
var userDbImages = (await ImageData.GetImagesByUser(userSecurityInfo.Id)).ToList();
更改頁面時仍在 DbContext 上執行,因此在新頁面上進行的任何 DB 呼叫都會失敗,因為唯一的 DbContext 已在使用中。我猜是因為在這一點上我不知道是什么ImageData!
DbContext Factory 解決了這些問題,因此您需要理清實作 Factory 時遇到的問題,因為您無法在真實應用程式中針對單個背景關系運行異步資料庫操作。發布有關您的工廠問題的問題,我們會看看我們是否可以提供幫助。
對設計的進一步評論:使用范圍視圖服務來保存影像資料,因此您只能獲得一次:如果我錯誤地閱讀此代碼并且您已經在ImageData.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/441671.html
