How can I use some of my services and repositories in job class of Quartz.net?

369 views Asked by At

I need to use some of my services in job class of Quartz.net I use Autofac as dependency injection

public class PushJob : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {                      
      // need to use some service here 

    }
}
1

There are 1 answers

3
Bobo On

If you seek the simplest solution, just make your PushJob class a starting point = composition root of your DI like this:

public class PushJob : IJob
{
    private IContainer _container;

    public async Task Execute(IJobExecutionContext context)
    {                      
      Register();
      DoWork();
    }
}

in Register() just create your container instance, register all dependencies and store in _container. Then in DoWork do something like:

var worker = _container.Resolve<IWorker>();

that will actually instantiate your worker with your service injected as needed.