Base solution for your next web application
Open Closed

Worker unable to query from repo #9173


User avatar
0
marble68 created

Using the SubscriptionExpireEmailNotifierWorker as a template, I've created a worker.

When it runs, it tries to call GetAll from a repository and I get an ObjectDisposedException error.

This is my worker:

  public class MyWorker : PeriodicBackgroundWorkerBase, ISingletonDependency
    {

        private const int CheckPeriodAsMilliseconds = 1000 * 1 * 60 * 30; // 30 minutes

        private readonly IWebUrlService _webUrlService;
        private readonly IRepository<Employee, Guid> _employeeRepository;

        public MyWorker(
                AbpTimer timer,
                IRepository<Employee, Guid> employeeRepository
                ) : base(timer)
        {

            _employeeRepository = employeeRepository;

            Timer.Period = CheckPeriodAsMilliseconds;
            Timer.RunOnStart = true;

        }

        protected override void DoWork()
        {
            List<Employee> e = _employeeRepository.GetAll().ToList();

            int c = e.Count();
        }
    }

This is the error I get:

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

This in the MVC startup, I register the worker like this:

workManager.Add(IocManager.Resolve<MyWorker>());

What am I missing?

Thanks!


1 Answer(s)
  • User Avatar
    0
    marble68 created

    Resolved - the DoWork method must be a [UnitOfWork]

    As per https://aspnetboilerplate.com/Pages/Documents/Background-Jobs-And-Workers