1

I have the Quartz Job shown below

    public class ExtractTradesJob: IJob
    {
        private ITradeExtractor _tradeExtractor;

        public ExtractTradesJob(ITradeExtractor tradeExtractor)
        {
            _tradeExtractor = tradeExtractor;
        }


        public async Task GetTradesAsync(DateTime dateTime)
        {
            Console.WriteLine(dateTime);
        }

        void IJob.Execute(IJobExecutionContext context)
        {
            Task.Run(async () => await GetTradesAsync(DateTime.Now));
        }
    }

I have 2 issues

  1. As I need to use .NET 4.5, Async support is not available out of the box so is the method I have used for calling my async method correct?
  2. It appears as though if I have a constructor as in my case, the job does not fire. I have checked and I know the the ITradeExtractor service is registered correctly. So how can I have a job that takes a service in its constructor? If I remove the constructor, my Execute method is called correctly

I am using AutoFac 3.5.2 and Quartz 2.6.2 and AutoFac.Extras.Quartz 3.5.0

I am using the code below to setup AutoFac

        public static void RegisterWithAutofac(ContainerBuilder builder)
        {
            builder.RegisterType<TradeExtractor>()
                .As<ITradeExtractor>()
                .SingleInstance();

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


        }

I know these are old packages, but my limitation of having to use .NET 4.5 means this is out of my control

Paul

walen
  • 7,103
  • 2
  • 37
  • 58
Paul
  • 2,773
  • 7
  • 41
  • 96
  • I assume this hold true for C# as in case of java quartz scheduler,quartz library uses reflection to create the instance of job and then call its execute method.So,this should explain why the code is called as expected without the constructor.You might need a jobParameter to pass on the parameters to the job. https://stackoverflow.com/questions/7137960/quartz-scheduler-how-to-pass-custom-objects-as-jobparameter – user06062019 Sep 12 '20 at 16:58

0 Answers0