• 记一次 ABP VNext 进程崩溃。错误源 System.ObjectDisposedException: Cannot access a disposed context instance.


    正在做 excel 导入功能,代码在自己的机器上正常运行,部署到 CentOS 后只要调用这块代码进程就直接崩掉,以下是问题代码:

    using (var stream = new MemoryStream())
    {
         ImportQuestionsFile.CopyTo(stream);
         var dt = NPOIHelper.ImportExceltoDt(stream);
         var list = new List<CreateUpdateQuestionDto>();
         dt.AsEnumerable().ToList().ForEach(x =>
         {
            ...MORE CODE...
            list.Add(question);
         });
         _questionAppService.InsertManyAsync(list);
         return NoContent();
    }
    

    开始以为和以前一样是进程占用内存太大导致,结果一顿排查进程不是被系统 kill 的。
    Logs 里面又没有日志,幸好使用了 Supervisor 守护进程,在它的日志里面找到的问题根源:

    Unhandled exception. System.ObjectDisposedException: Cannot access a disposed context instance. A common cause of this error is disposing a context instance 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' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
    Object name: 'AssociationDbContext'.
       at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
       at Microsoft.EntityFrameworkCore.DbContext.get_ContextServices()
       at Microsoft.EntityFrameworkCore.DbContext.get_ChangeTracker()
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.OnTracked(InternalEntityEntry internalEntityEntry, Boolean fromQuery)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.FireStateChanged(EntityState oldState)
    

    根据日志分析发现是 DbContext 被 disposed 。最后排查是因为 _questionAppService.InsertManyAsync(list); 这个异步方法没有加 await 程序就不会等待。但不知道为啥进程会崩溃。

  • 相关阅读:
    挖矿程序linux 删除
    本地复制vue项目
    新建vue项目
    CentOS7单用户模式
    CentOS6
    CentOS6-系统管理操作
    CentOS7-系统管理操作
    VMWare克隆虚拟机
    虚拟机网络模式设置为NAT
    VI/VIM编辑器
  • 原文地址:https://www.cnblogs.com/yingcheng/p/16047331.html
Copyright © 2020-2023  润新知