作者:风云变幻1892_602 | 来源:互联网 | 2022-12-05 14:40
我正在尝试在应用程序启动时设置Hangfire经常性作业,但是在该作业中,如果我尝试使用IRepository
Injection,它将在尝试执行查询时给出与DbContext相关的错误:
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 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. Object name: 'WsApplicationDbContext'.
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 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.
Object name: 'WsApplicationDbContext'.
at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)
at WsApplication.RecurringJobs.Jobs.ContainerIsBookedOffHireRecurringJob.TestJob() in C:\Projects\boxman\aspnet-core\src\WsApplication.Application\RecurringJobs\Jobs\ContainerIsBookedOffHireRecurringJob.cs:line 26
我已经按照ABP文档中的说明完全注册了Hangfire 。另外,我正在这样在.Web.Core
模块中注册我的工作:
public override void PostInitialize()
{
...
// set up recurring background jobs
var job = IocManager.Resolve();
RecurringJob.AddOrUpdate("TestJobWithRepository", j => j.TestJob(), Cron.Daily);
}
我的工作方法只是尝试使用注入的存储库进行查询。
public class ContainerIsBookedOffHireRecurringJob : BaseRecurringJob
{
private readonly IRepository _contractLineMovementRepository;
public ContainerIsBookedOffHireRecurringJob(
IIocResolver iocResolver,
IRepository contractLineMovementRepository
) : base(iocResolver)
{
_cOntractLineMovementRepository= contractLineMovementRepository;
}
public void TestJob()
{
var testQuery = from contractLineMovement in _contractLineMovementRepository.GetAll()
select contractLineMovement;
testQuery.ToList().ForEach(t =>
{
Logger.Debug("Fetched ContractLineMovement ID: " + t.Id + " Type: " + t.ContractWorkFlowEventId);
});
}
}
我设法通过WsApplicationDbContext
直接解决来解决此问题,BaseRecurringJob
但我不太喜欢这种解决方案。我不明白为什么它不能简单地与依赖注入一起使用。是因为我要在启动时注册它们吗?我需要某种自定义的Job Activator吗?
任何帮助将非常感激。
1> aaron..:
添加[UnitOfWork]
属性并使其成为virtual
方法:
[UnitOfWork]
public virtual void TestJob()
{
...
}
请参阅:UnitOfWork属性限制
您可以将UnitOfWork属性用于:
在接口上使用的类的所有公共或公共虚拟方法(例如在服务接口上使用的应用程序服务)。
用于自注入类的所有公共虚拟方法(如MVC控制器和Web API控制器)。
所有受保护的虚拟方法。