在定时任务中,使用Repository 执行预定任务遇到报错:System.ObjectDisposedException: 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 is 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. Object name: ‘XXXXXDBContext’。
报错信息大意就是在使用了已经被释放的对象。主要原因当然是在DI的时候没有控制好释放的时机。好吧,我就根本没有控制。导致在使用前Repository就被释放了。
经过我勤奋的搜索,找到一个有效的方法:方法前加[UnitOfWork]属性。