无法访问 Blazor Server 3.0 中已处理的对象 [英] Cannot access a disposed object in Blazor Server 3.0

查看:29
本文介绍了无法访问 Blazor Server 3.0 中已处理的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下表单提交按钮的 Blazor 页面

<button type="submit" class="btn btn-primary" @onclick="@(async()=> await OnCreateItem())">创建</button>

哪个电话

protected async Task OnCreateItem(){尝试{var 结果 = 等待 dbMgr.CreateAsync(item);}捕获(异常前){抛出 ==>无法访问已处置的对象.此错误的一个常见原因是处理从依赖注入解析的上下文,然后尝试在应用程序的其他地方使用相同的上下文实例.如果您正在调用 Dispose() ,则可能会发生这种情况...}}

dbMgr 注入页面

@inject DBManager dbMgr;

管理器实现为

class DBManager{...公共 DBManager(DBContext 数据库){_db = db;}公共异步任务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();}

我在这里阅读了类似的问题链接 1链接 2

无济于事.

不确定我做错了什么.

解决方案

我没有尝试你的代码,但我猜是由于在

中设置了 type="submit"

<button type="submit" class="btn btn-primary" @onclick="@(async()=> await 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆