无法访问Blazor Server 3.0中的已处置对象 [英] Cannot access a disposed object in Blazor Server 3.0
问题描述
我有一个带有以下表单提交按钮的Blazor页面
< button type = submit class = btn btn -primary @onclick = @(async()=>等待OnCreateItem())>创建< / button>
哪个通话
受保护的异步任务OnCreateItem()
{
try
{
var result = await dbMgr.CreateAsync(item);
}
捕获(异常除外)
{
抛出==>无法访问已处置的对象。导致此错误的常见原因是,处理从依赖项注入中解决的上下文,然后稍后尝试在应用程序中的其他位置使用相同的上下文实例。如果您正在调用Dispose()...
}
}
dbMgr注入到页面
@inject DBManager dbMgr;
管理器的实现方式为
class DBManager
{
...
public DBManager(DBContext db)
{
_db = db;
}
公共异步任务< int> CreateAsync(ItemModel obj)
{
等待_db.AddAsync(obj);
等待返回_db.SaveChangesAsync()
}
}
启动类为
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddDbContext< DBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString( DBContext)),ServiceLifetime.Scoped);
services.AddScoped< DBManager>();
}
无济于事。
不确定我在做什么错。
我没有尝试您的代码,但是我想这是由于在
<$ p $中设置type = submit p>
<按钮类型=提交 class = btn btn-primary @onclick = @(async()=>等待OnCreateItem())>创建< / button>
结果,当您按下创建按钮时,就会发生传统的回发。 ..也就是说,您的SPA应用回发到服务器。这种情况不应该发生(在这种情况下),因为您的应用重定向到限制基本uri之外的uri,然后返回,令人遗憾地发现OnCreateItem中的对象已失效(已处置)。只需设置type = button。
重要提示:据我回忆起过去,Blazor JavaScript中应该有代码可以防止此类事件发生:即回发到服务器。
I got a Blazor page with following form submit button
<button type="submit" class="btn btn-primary" @onclick="@(async()=> await OnCreateItem())">Create </button>
Which calls
protected async Task OnCreateItem()
{
try
{
var result = await dbMgr.CreateAsync(item);
}
catch (Exception ex)
{
Throws ==> Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose()...
}
}
dbMgr is injected to the page
@inject DBManager dbMgr;
And manager is implemented as
class DBManager
{
...
public DBManager(DBContext db)
{
_db = db;
}
public async Task<int> CreateAsync(ItemModel obj)
{
await _db.AddAsync(obj);
return await _db.SaveChangesAsync()
}
}
Startup class is
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddDbContext<DBContext >(options =>
options.UseSqlServer(Configuration.GetConnectionString("DBContext")), ServiceLifetime.Scoped);
services.AddScoped<DBManager>();
}
I read a similar questions here Link 1 Link 2
To no avail.
Not sure what am I doing wrong.
I did not try your code, but I guess it is due to setting type="submit" in
<button type="submit" class="btn btn-primary" @onclick="@(async()=> await OnCreateItem())">Create </button>
As a result, when you hit the "Create" button, a traditional post back takes place. .. that is, your SPA app post back to the server. It shouldn't happen (in that case), as your app redirects to a uri outside of the limiting base uri, and then returns back, sadly enough to find the objects in the OnCreateItem dead (disposed). Just set type="button".
Important: As far as I recall from days bygone, there should be code in Blazor JavaScript to prevent such incidents: that is posting back to the server.
这篇关于无法访问Blazor Server 3.0中的已处置对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!