Search code examples
c#quartz-schedulerquartz.net

Migrating from Quartz.NET 2 to 3 Casting Error


My question is regarding the upgrade from Quartz.Net 2 to 3 in which now includes await/async methods. I have followed all the instructions in the migration document but I'm having an issue at the following initializaiton (marked line):

 public partial class BMOSSService : ServiceBase
{
    private EventLogger _eventLogger = new EventLogger("BMOSS");
    private static ISchedulerFactory _scheduleFactory = new StdSchedulerFactory();
    ****private static IScheduler _scheduler = _scheduleFactory.GetScheduler();****

    private static readonly ILog _log = LogManager.GetLogger("BlackBox.BMOSS.Scheduler");
    private static readonly ILog _logToDo = LogManager.GetLogger("BlackBox.BMOSS.Scheduler.ToDo");

    private static bool _forceStop = false;

    public BMOSSService() ...

I understand that the scheduler factory now needs the await instruction but the issue is that this is a global declaration and I can add the asynch keyword to the class how can I fix this? any ideas?

Thanks


Solution

  • Finally I resolved the issue. This is how I did it. First I had to change the variable initialization from:

    private static IScheduler _scheduler = _scheduleFactory.GetScheduler();
    

    to

    private static Task<IScheduler> _scheduler = _scheduleFactory.GetScheduler();
    

    This made me change some other method implementations to return a Task and some await and everything is running now. The trick was to receive the value like this:

     public async Task RegisterJobsProcessAsync(Task<IScheduler> scheduler)
            {
                _log.Info("Job registering process begins");
    
                this._scheduler = scheduler.Result;
    
                await UnRegisterJobsAsync();
                await RegisterJobsAsync();
    
                _log.Info("Job registering process ends");
            }