问题
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
回答1:
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");
}
回答2:
In the new version, GetScheduler()
returns a Task<IScheduler>
, which I didn't understand at first.
In this case, you just want the result of your Task (i.e. just your IScheduler
value), which can read as follows:
private static IScheduler _scheduler = _scheduleFactory.GetScheduler().Result;
Here is the official documentation.
来源:https://stackoverflow.com/questions/49449685/migrating-from-quartz-net-2-to-3-casting-error