首页 新闻 赞助 找找看

.net core异常:"Cannot access a disposed object"

0
悬赏园豆:30 [已解决问题] 解决于 2018-02-05 22:22

在一个asp.net core webapi项目的集成测试中遭遇下面的异常,请问如何解决?

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() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
[xUnit.net 00:00:03.2384249]       Object name: 'EfUnitOfWork'.
[xUnit.net 00:00:03.2425838]       Stack Trace:

依赖注入用的是 services.AddScoped

dudu的主页 dudu | 高人七级 | 园豆:31075
提问于:2018-02-05 20:51
< >
分享
最佳答案
1

翻转控制器已经释放了目标对象了,参考修改对象生命周期的方法

收获园豆:30
hellozmh | 初学一级 |园豆:29 | 2018-02-05 21:56

有个 Repository 实现了 IDisposable 接口,在实现中调用了 _unitOfWork.Dispose() 方法,从而引起这个问题

dudu | 园豆:31075 (高人七级) | 2018-02-05 22:22
其他回答(1)
1

我现在也碰到了这样的错误。业务场景是Dbcontext,注入的时候生命周期是LifeScope。在业务中开启了多个T异步Task去执行操作DbContext,导致这样的错误。
但是我在获取DbContext的时候,是创建了Scope之后。这样也会出现线程问题吗?

Code_Song | 园豆:51 (初学一级) | 2019-01-14 15:08

建议贴出相关代码

支持(0) 反对(1) dudu | 园豆:31075 (高人七级) | 2019-01-14 20:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册