0

I have added scheduler in Startup of service like:

services.AddQuartz(q =>
{
    q.SchedulerId = "S1";
    q.SchedulerName = "S1";
    
    q.UseMicrosoftDependencyInjectionJobFactory();

    q.UsePersistentStore(s =>
    {
        s.UseProperties = true;
        s.UsePostgres("ConnectionString");
      
        s.UseJsonSerializer();
    });
})

Now I am tring to use this created Scheduler via DI like:

public SchedulerStartup(ISchedulerFactory schedulerFactory)
{
    this.schedulerFactory = schedulerFactory;
}

public async Task StartAsync(CancellationToken cancellationToken)
{
    Scheduler = await schedulerFactory.GetScheduler("S1", cancellationToken);
    
    await Scheduler.Start(cancellationToken);
}

But somehow Scheduler is null. I wont able to access created Scheduler in startup configuration (S1).

Link: https://www.quartz-scheduler.net/documentation/quartz-3.x/packages/microsoft-di-integration.html#di-aware-job-factories

Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
  • 1
    What interface class did you use to implement? You can read this post, it may be helpful to you. https://stackoverflow.com/questions/42157775/net-core-quartz-dependency-injection – Tupac Sep 22 '21 at 02:53
  • 1
    Just call GetScheduler without specifying the ID, it will return the one and only you've configured. – Marko Lahma Sep 22 '21 at 10:08
  • Here I have missed `services.AddQuartzHostedService()` to start scheduler using Hosting Service. Additional startup class not required. @MarkoLahma – Ankush Madankar Sep 23 '21 at 06:22

1 Answers1

1

Here I have missed services.AddQuartzHostedService() to start scheduler using Hosting Service. Additional startup class not required.

This should be like this:

services.AddQuartz(q =>
 {
     q.SchedulerName = "S1";

     q.UseMicrosoftDependencyInjectionJobFactory();
     
     q.UsePersistentStore(s =>
     {
         s.UseProperties = true;
         s.UsePostgres(DbConnectionString);
         s.UseJsonSerializer();
     });
 });

 services.AddQuartzHostedService(options =>
 {
     options.WaitForJobsToComplete = true;
 });

Later created instance of this Scheduler can be used as (S1):

public MyRuntimeScheduler(ISchedulerFactory schedulerFactory)
{
     Scheduler = schedulerFactory.GetScheduler("S1").GetAwaiter().GetResult();
}
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74