ASP.Net Core MVC存储库模式意外处置

丹尼尔·C

当我尝试添加评论时,出现以下错误:

ObjectDisposedException:无法访问已处置的对象。

当代码运行第二行时:

m_context.Comments.Add(comment);
m_context.SaveChanges();

为什么要处理上下文?如果将TryAddComment方法移入控制器,则不会提早调用Dispose。

这是我的Controller和Repository类的样子(简化)。

CommentsController.cs:

public class CommentsController : Controller
{

    private ICommentRepository m_commentRepository;

    public CommentsController(ICommentRepository commentRepository)
    {
        m_commentRepository = commentRepository;
    }

    // POST: api/Comments
    [HttpPost]
    public async Task<IActionResult> PostComment([FromBody] CommentAddViewModel commentVM)
    {
        Comment comment = new Comment
        {
            ApplicationUserId = User.GetUserId(),
            PostId = commentVM.PostId,
            Text = commentVM.Text
        };

        bool didAdd = m_commentRepository.TryAddComment(comment);

        if (!didAdd)
        {
            return new HttpStatusCodeResult(StatusCodes.Status409Conflict);
        }

        return CreatedAtRoute("GetComment", new { id = comment.CommentId }, comment);
    }

}

CommentRepository.cs:

public class CommentRepository : ICommentRepository, IDisposable
{

    public ApplicationDbContext m_context;

    public CommentRepository(ApplicationDbContext context)
    {
        m_context = context;
    }
    public bool TryAddComment(Comment comment)
    {
        m_context.Comments.Add(comment);
        m_context.SaveChanges();

        return true;
    }
    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                m_context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

编辑:

如果我使用本地CommentRepository,它将按预期工作。例如:

    CommentRepository localCommentRepo = new CommentRepository(m_context);
    bool didAdd = localCommentRepo.TryAddComment(comment);

编辑2:

在Startup.cs中,我将IcommentRepository注册为Scoped并按预期工作。最初是Singleton。为什么单身人士会导致此问题?

services.AddSingleton<ICommentRepository, CommentRepository>(); //breaks
services.AddScoped<ICommentRepository, CommentRepository>(); //works

编辑3:

ApplicationDbContext.cs:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

    }
    public DbSet<Post> Posts { get; set; }
    public DbSet<Comment> Comments { get; set; }
}

您的存储库和您的存储库都不DbContext应该是单例。注册它们的正确方法是services.AddScopedservices.AddTransient,因为DbContext生存期不应长于请求,而AddScoped恰恰是为此目的。

AddScopedDbContext在范围的生命周期(在ASP.NET Core中等于请求的生命周期)的范围内返回一个(和存储库,如果您以此方式注册的)相同实例

使用时,AddScope您不应该自己处置上下文,因为解析存储库的下一个对象将具有处置的上下文。

实体框架默认情况下将上下文注册为作用域,因此您的存储库应为作用域(与上下文和请求相同的生存期)或临时的(每个服务实例获取其自己的存储库实例,但请求中的所有存储库仍共享相同的存储库)语境)。

使上下文单例会导致严重的问题,尤其是在内存方面(您处理的越多,上下文消耗的内存越多,因为它必须跟踪更多的记录)。因此,aDbContext应该尽可能短命。

上下文的持续时间的优势在于,如果出现问题,您仍然可以回滚请求期间的所有操作,并将其作为一个事务处理。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在ASP.NET Core中处置ApplicationDbContext

来自分类Dev

实体框架和ASP.NET Core UOW存储库模式

来自分类Dev

如何使用ASP.NET Core中的存储库模式进行排序?

来自分类Dev

与ASP.NET MVC BeginExecuteCore等效的ASP.NET Core

来自分类Dev

ASP.Net Core MVC CRUD弹出模式

来自分类Dev

ASP.NET Core MVC 引导模式 - 不同的内容

来自分类Dev

使用ASP.NET Core和MVC存储本地文件

来自分类Dev

ASP.NET Core MVC视图附件

来自分类Dev

ASP.NET MVC 6(ASP.NET Core或ASP.NET5)中的友好URL

来自分类Dev

ASP.NET Core MVC 和 EF Core 1.1

来自分类Dev

DbContext实例何时在ASP.NET Core 5中处置

来自分类Dev

ASP.NET CORE 1.0存储库加载相关数据EF CORE

来自分类Dev

是否可以从ASP.NET Core MVC项目中引用.net Framework 4.7.2类库?

来自分类Dev

在ASP.NET MVC中实现存储库模式和unitOfWork的最佳实践

来自分类Dev

没有存储库模式的ASP.Net MVC和实体框架项目体系结构

来自分类Dev

ASP.NET 5 MVC 6通用存储库模式

来自分类Dev

如何在n层存储库模式中共享DbCOntext。ASP .NET MVC

来自分类Dev

ASP.NET CORE身份UserManager CreateAsync模式错误

来自分类Dev

访问 ASP.NET Core 中的配置选项:选项模式

来自分类Dev

开发模式asp.net core中的详细信息

来自分类Dev

查看ASP.NET MVC与ASP.NET Core MVC之间的目录编译时间差

来自分类Dev

在ASP .NET Core中何处存储私有映像

来自分类Dev

无法添加对.net core类库asp.net core rc2的引用

来自分类Dev

从.NET Core / ASP.NET Core中的类库访问App关键数据

来自分类Dev

无法在ASP.NET Core项目中添加对.NET Core类库的引用

来自分类Dev

数据库的ASP.NET Core ILoggerProvider

来自分类Dev

如何在ASP.NET Core中移动凉亭库?

来自分类Dev

ASP.NET Core 2.1 从类库中引用 ApplicationDbContext

来自分类Dev

如何创建 ASP.NET Core 类库

Related 相关文章

  1. 1

    在ASP.NET Core中处置ApplicationDbContext

  2. 2

    实体框架和ASP.NET Core UOW存储库模式

  3. 3

    如何使用ASP.NET Core中的存储库模式进行排序?

  4. 4

    与ASP.NET MVC BeginExecuteCore等效的ASP.NET Core

  5. 5

    ASP.Net Core MVC CRUD弹出模式

  6. 6

    ASP.NET Core MVC 引导模式 - 不同的内容

  7. 7

    使用ASP.NET Core和MVC存储本地文件

  8. 8

    ASP.NET Core MVC视图附件

  9. 9

    ASP.NET MVC 6(ASP.NET Core或ASP.NET5)中的友好URL

  10. 10

    ASP.NET Core MVC 和 EF Core 1.1

  11. 11

    DbContext实例何时在ASP.NET Core 5中处置

  12. 12

    ASP.NET CORE 1.0存储库加载相关数据EF CORE

  13. 13

    是否可以从ASP.NET Core MVC项目中引用.net Framework 4.7.2类库?

  14. 14

    在ASP.NET MVC中实现存储库模式和unitOfWork的最佳实践

  15. 15

    没有存储库模式的ASP.Net MVC和实体框架项目体系结构

  16. 16

    ASP.NET 5 MVC 6通用存储库模式

  17. 17

    如何在n层存储库模式中共享DbCOntext。ASP .NET MVC

  18. 18

    ASP.NET CORE身份UserManager CreateAsync模式错误

  19. 19

    访问 ASP.NET Core 中的配置选项:选项模式

  20. 20

    开发模式asp.net core中的详细信息

  21. 21

    查看ASP.NET MVC与ASP.NET Core MVC之间的目录编译时间差

  22. 22

    在ASP .NET Core中何处存储私有映像

  23. 23

    无法添加对.net core类库asp.net core rc2的引用

  24. 24

    从.NET Core / ASP.NET Core中的类库访问App关键数据

  25. 25

    无法在ASP.NET Core项目中添加对.NET Core类库的引用

  26. 26

    数据库的ASP.NET Core ILoggerProvider

  27. 27

    如何在ASP.NET Core中移动凉亭库?

  28. 28

    ASP.NET Core 2.1 从类库中引用 ApplicationDbContext

  29. 29

    如何创建 ASP.NET Core 类库

热门标签

归档