1

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 

    }
}
Ali Tanha
  • 13
  • 5
  • 1
    This should help https://stackoverflow.com/questions/4910131/autofac-and-quartz-net-integration – cl0ud Nov 06 '18 at 08:36

1 Answers1

0

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.

Bobo
  • 335
  • 2
  • 10
  • this not work and error was this "The request lifetime scope cannot be created because the HttpContext is not available" – Ali Tanha Nov 06 '18 at 10:46
  • HttpContext is a System.Web construct. It is used in web applications like ASP.NET. On the contrary the Quartz is not connected to ASP.NET at all, it is like running a console application on specified time. You have no HttpContext available by default. – Bobo Nov 06 '18 at 11:52
  • https://autofaccn.readthedocs.io/en/latest/faq/per-request-scope.html may help you to clarify. If you have control over your registrations, I would just not pick the Per-Request lifetime in this case as you do not have any request to work with... If you can register with InstancePerLifetimeScope you should be fine. – Bobo Nov 06 '18 at 11:58