How do I create a Quartz.NET’s job requiring injection with autofac
Asked Answered
T

2

17

I am trying to get Quartz.net (2.1.2) to work with an IoC container (autofac), as I have services I need to use in the scheduled jobs. I have found similar posts on the subject, but I can't seem to find one with a specific registration example for autofac.

The following post deals with the same issue I am having:

How to schedule task using Quartz.net 2.0?

However, the part I believe I am missing is when the answer says "And don't forget to register the job in the IoC container". I am unsure how to do this exactly, as everything I have tried so far hasn't worked.

In the following example, the "HelloJob" will run, but whenever I try to inject the releaseService into the "ReleaseJob" it refuses to run.

Update: I marked the code in the DependencyRegistration.cs section where I believe the issue is.

Update 2: Some related links that are related to what I need to do and might help (I've already gone through them all but still cannot figure out how to get this working with autofac):

HOW TO use Quartz.NET in PRO way? - http://blog.goyello.com/2009/09/21/how-to-use-quartz-net-in-pro-way/

Autofac and Quartz.NET - http://blog.humann.info/post/2013/01/30/Autofac-and-QuartzNET.aspx

Constructor injection with Quartz.NET and Simple Injector - Constructor injection with Quartz.NET and Simple Injector

ASP.Net MVC 3, Ninject and Quartz.Net - How to? - ASP.Net MVC 3, Ninject and Quartz.Net - How to?

Here is the relevant code:

Global.asax

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        var dependencyRegistration = new DependencyRegistration();
        dependencyRegistration.Register();

        ModelValidatorProviders.Providers.Clear();
        ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new ValidatorFactory()));

        DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
    }

DependencyRegistration.cs

public class DependencyRegistration
{
    public void Register()
    {
        var builder = new ContainerBuilder();

        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());

        // Validation
        builder.RegisterType<ValidatorFactory>()
            .As<IValidatorFactory>()
            .InstancePerHttpRequest();

        AssemblyScanner findValidatorsInAssembly = AssemblyScanner.FindValidatorsInAssembly(Assembly.GetExecutingAssembly());
        foreach (AssemblyScanner.AssemblyScanResult item in findValidatorsInAssembly)
        {
            builder.RegisterType(item.ValidatorType)
                .As(item.InterfaceType)
                .InstancePerHttpRequest();
        }

        // Schedule
        builder.Register(x => new StdSchedulerFactory().GetScheduler()).As<IScheduler>();

        // Schedule jobs
        builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(x => typeof(IJob).IsAssignableFrom(x));

        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

        //Schedule
        IScheduler sched = container.Resolve<IScheduler>();
        sched.JobFactory = new AutofacJobFactory(container);
        sched.Start();

        IJobDetail job = JobBuilder.Create<ReleaseJob>()
                .WithIdentity("1Job")
                .Build();

        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("1JobTrigger")
            .WithSimpleSchedule(x => x
                .RepeatForever()
                .WithIntervalInSeconds(5)
            )
            .StartNow()
            .Build();

        sched.ScheduleJob(job, trigger);

        job = JobBuilder.Create<HelloJob>()
               .WithIdentity("2Job")
               .Build();

        trigger = TriggerBuilder.Create()
            .WithIdentity("2JobTrigger")
            .WithSimpleSchedule(x => x
                .RepeatForever()
                .WithIntervalInSeconds(5)
            )
            .StartNow()
            .Build();

        sched.ScheduleJob(job, trigger);
    }
}

JobFactory.cs

public class AutofacJobFactory : IJobFactory
{
    private readonly IContainer _container;

    public AutofacJobFactory(IContainer container)
    {
        _container = container;
    }

    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
    {
        return (IJob)_container.Resolve(bundle.JobDetail.JobType);
    }

    public void ReturnJob(IJob job)
    {
    }
}

ReleaseJob.cs

public class ReleaseJob : IJob
{
    private readonly IReleaseService _releaseService;

    public ReleaseJob(IReleaseService releaseService)
    {
        this._releaseService = releaseService;
    }

    public void Execute(IJobExecutionContext context)
    {
        Debug.WriteLine("Release running at " + DateTime.Now.ToString());
    }
}

public class HelloJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Debug.WriteLine("Hello job at " + DateTime.Now.ToString());
    }
}

ReleaseServiceModel.cs

public class ReleaseServiceModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<ReleaseService>()
            .As<IReleaseService>()
            .InstancePerLifetimeScope();
    }
}
Tedmund answered 4/6, 2013 at 1:16 Comment(1)
You saved me the time of figuring this out. Thanks a lot Tomas.Trueman
T
6

I finally tracked down what the issue was.

My release service was using a data repository which was being created with a different scope.

I discovered this by creating a new test service that did nothing but return a string, and that worked being injected into a quartz job.

On discovering this, I changed the scope of the repository called upon by the release service , and then the release service started working inside the quartz job.

My apologies to anyone that looked at this question to try and help me out. Because the code of the release service was not listed, it would have been difficult to figure out what was wrong.

I have updated the code with the final working bindings I used for quartz with autofac.

Tedmund answered 10/6, 2013 at 22:39 Comment(2)
Hi Thomas - Sorry to bring back up an old post, but have you got an example of how you changed the scope of your service to account for this. I am falling into the same trap where im not too experienced in dependancy injection and cant get this to work :/Quits
I believe it was changing the scope from "InstancePerHttpRequest" which was not working, to "InstancePerLifetimeScope" which did work, in the repository and releaseservice modules.Tedmund
J
0

The problem is that your AutofacJobFactory is not creating the ReleaseJob (you are doing this with JobBuilder.Create<ReleaseJob>() instead), so the IoC container is not aware of it's instantiation meaning dependency injection cannot occur.

The following code should work:

sched = schedFact.GetScheduler();
sched.JobFactory = new AutofacJobFactory(container);
sched.Start();

// construct job info
JobDetailImpl jobDetail = new JobDetailImpl("1Job", null, typeof(ReleaseJob ));

ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("1JobTrigger")
    .WithSimpleSchedule(x => x
        .RepeatForever()
        .WithIntervalInSeconds(5)
    )
    .StartNow()
    .Build();

sched.ScheduleJob(jobDetail, trigger);

Note that in this example we are not using JobBuilder.Create<ReleaseJob>() anymore, instead we pass the details of the job to be created via the JobDetailImpl object and by doing this the scheduler's jobfactory (the AutofacJobFactory) is responsible for instantiating the job and Dependency injection can occur.

Jacquelyn answered 4/6, 2013 at 3:56 Comment(3)
I made the change you recommended. I removed the JobBuilder.Create<> and replaced it with your example. However, the behavior is still the same. The HelloJob runs but the ReleaseJob does not.Tedmund
I assume that the AutofacJobFactory.NewJob() method is now being called? If so, Its a problem with your binding. As a test can you change the relevant marked issue lines to builder.RegisterType<ReleaseJob>.As<IJob>(); and also change the next line to JobDetailImpl jobDetail = new JobDetailImpl("1Job", null, typeof(IJob )); Note that this will test whether bindings are now being resolved, but you'll need an abstract factory if you want to instantiate multiple types of IJobJacquelyn
I put a breakpoint on the AutofacJobFactory.NewJob(). It does get called for the HelloJob, but never seems to trigger from the ReleaseJob. I also changed the builder and job types as noted to IJob and ran them only one at a time. The HelloJob worked again, the ReleaseJob did not. Thank you for all the help so far--I'm not very experienced with bindings and have suspected they were the issue here all along, I'm just not sure what to try next to get them to work.Tedmund

© 2022 - 2024 — McMap. All rights reserved.