StructureMap with Quartz.Net not working

I am trying to implement Quartz.Net with StructureMap, but the job does not start. My code is as follows.

public class SampleRegistry : Registry
    {
        public SampleRegistry()
        {
            Scan(x =>
            {
                x.AssembliesFromApplicationBaseDirectory();
                x.WithDefaultConventions();
                x.AddAllTypesOf<IJob>().NameBy(c => c.Name);
            });
            For<IJobFactory>().Use<StructureMapJobFactory>();

            For<ISchedulerFactory>().Use(ctx => new StdSchedulerFactory());
            For<IScheduler>().Use(delegate(IContext ctx)
            {
                var scheduler = ctx.GetInstance<ISchedulerFactory>().GetScheduler();
                scheduler.JobFactory = ctx.GetInstance<IJobFactory>();
                return scheduler;
            });
        }
    }

StructureMapJobFactory code looks like

public class StructureMapJobFactory : IJobFactory
{
    private readonly IContainer container;

    public StructureMapJobFactory(IContainer container)
    {
        this.container = container;
    }

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

The sample job looks like

 public class SendMailJob : IJob
{
    private readonly IMailService _mailService;

    public SendMailJob(IMailService mailService)
    {
        _mailService = mailService;
    }

    public void Execute(IJobExecutionContext context)
    {
        _mailService.SendMail();
    }
}

And JobDetails

IScheduler scheduler = ObjectFactory.GetInstance<IScheduler>();
        scheduler.Start();

        IJobDetail job = JobBuilder.Create(typeof(Code.Services.SendMailJob))
                                   .WithIdentity("sampleJob", "group1")
                                   .Build();

        ITrigger trigger = TriggerBuilder.Create()
                                         .WithIdentity("sampleTrigger", "group1")
                                         .StartNow()
                                         .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever())
                                         .Build();


        scheduler.ScheduleJob(job, trigger);
+3
source share

All Articles