我正在開發一個Blazor服務器應用程序,所有的客戶將有一個名單things
,這些客戶中的任何一個可以更新thing
那麼應該觸發回調,告訴所有的客戶端調用DbContext.Entry(thing).Reload()
所以他們'重新更新。直到刷新頁面,這一切都很好,並且很好,然後出現Cannot access a disposed object
錯誤,並且我不知道如何解決該問題。
我有以下服務:
services.AddDbContextPool<MainDbContext>(...);
services.AddSingleton<RefreshService>();
RefreshService.cs:
public class RefreshService {
public Func<long, Task> OnRefreshThing { get; set; }
public void RefreshThing(long thingId) => OnRefreshThing?.Invoke(thingId);
}
Index.blazor:
protected override void OnInitialized() {
RefreshService.OnRefreshIssue += OnRefreshIssue;
}
private async Task OnRefreshThing(long thingId) {
// This works perfectly until I refresh the page & try to call it again
Thing thing = await MainDbContext.Things.FindAsync(thingId); // exception is thrown here
await MainDbContext.Entry(thing).ReloadAsync();
}
這是觸發錯誤的示例:
Thing thing = Things.Where(t => t.ThingId == 1);
thing.Name = "New name";
RefreshService.RefreshThing(thing.ThingId);