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 

    }
}
1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

this not work and error was this "The request lifetime scope cannot be created because the HttpContext is not available"
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.