Migrating from Quartz.NET 2 to 3 Casting Error

♀尐吖头ヾ 提交于 2020-02-24 11:37:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!