在Hangfire中解析资料库

时间:2018-12-03 07:50:10

标签: aspnetboilerplate hangfire

我正在使用RecurringJob创建Hangfire作业,我需要访问存储库并在数据库上执行一些操作,但是它无法解决依赖项出现异常的情况。

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: 'CSBDbContext'.

Startup.cs

 var recurrentJobs = new RecurrentJobs.RecurrentJobs(IocManager.Instance);            
 RecurringJob.AddOrUpdate(() => recurrentJobs.LaunchJobsRecurrentDaily(), Cron.Minutely);

RecurrentJobs.cs

public class RecurrentJobs
{
    private readonly IIocResolver _iocResolver;
    public RecurrentJobs(IIocResolver iocResolver)
    {
        _iocResolver = iocResolver;
    }
    public async Task LaunchJobsRecurrentDaily()
    {
        try
        {
            using (var scope = _iocResolver.CreateScope())
            {
                var tempCartManager = scope.Resolve<IRepository<AppTempShoppingCart>>();
                var tempCartList= tempCartManager.GetAll().ToList(); //Getting exception here
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
}

我尝试删除使用自动换行作用域,但是得到了相同的异常消息。

0 个答案:

没有答案