每次使用时都会出现Quartz.NET错误

问题描述 投票:2回答:4

我在这行中得到一个错误说:

无法将类型'System.Threading.Tasks.Task'隐式转换为'Quartz.IScheduler'。存在显式转换(您是否错过了演员?)

如何解决;我不明白?请帮忙!

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

   public static void Start()
    {
        try
        {
            //Construct scheduler factory
            //IScheduler scheduler = schedulerFactory.GetScheduler();

           // IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();

            IJobDetail job = JobBuilder.Create<HelloJob>()
                .WithIdentity("jobName", "jobGroup")
                .Build();

            ITrigger trigger = TriggerBuilder.Create()
                .WithSimpleSchedule(s => s.WithIntervalInSeconds(60).RepeatForever())
                .StartNow()
                .Build();

            scheduler.ScheduleJob(job, trigger);

           // scheduler.Start();

        }

        catch (SchedulerException se)
        {
            //Console.WriteLine(se);
        }
    }
}

public class HelloJob : IJob
{
    private TBPESContext db = new TBPESContext();
    public void Execute(IJobExecutionContext context)
    {
        var AuthorName = from twitterAccount in db.Twitter_Account
                         from c in twitterAccount.Courses
                         select twitterAccount.Author_Name;

        foreach (var item in AuthorName)
        {
            TweetCrawler.SaveTweets(item);
        }


        throw new NotImplementedException();
    }
}
triggers quartz-scheduler scheduler quartz.net quartz
4个回答
8
投票

从版本3.0.0 Quartz:https://www.quartz-scheduler.net/2017/12/30/quartznet-3.0-released.html

  • SimpleThreadPool消失了,老拥有的线程消失了
  • IJob界面现在返回一个任务

所以我把这个例子用在这里:

class Program
{
    static void Main(string[] args)
    {
        JobScheduler jobScheduler = new JobScheduler();
        jobScheduler.Start();
        Console.ReadLine();
    }
}
public class JobScheduler
{
     public async void Start()
    {
        ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
        IScheduler scheduler = await schedulerFactory.GetScheduler();
        await scheduler.Start();

        IJobDetail job = JobBuilder.Create<HelloJob>().Build();

        ITrigger trigger = TriggerBuilder.Create()

            .WithIdentity("HelloJob ", "GreetingGroup")

            .WithCronSchedule("0 0/1 * 1/1 * ? *")

            .StartAt(DateTime.UtcNow)

            .WithPriority(1)

            .Build();

        await scheduler.ScheduleJob(job, trigger);

    }

}
public class HelloJob : IJob
{
    Task IJob.Execute(IJobExecutionContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        Task taskA = new Task(() => Console.WriteLine("Hello from task at {0}", DateTime.Now.ToString()));
        taskA.Start();
        return taskA;
    }
}

3
投票

我遇到了同样的问题。当我从Manage NuGet Packages加载石英时,它给了我版本:3.0.0-alpha2(Prerelease)。这导致您当前看到的错误。我在https://www.nuget.org/packages/Quartz/2.3.3发现了一个早期版本,按照说明安装它,重建我的程序,它就像它应该的那样工作。


2
投票

我无法完美解释,但我知道它是如何工作的。

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

你需要得到GetDefaultScheduler()so的结果:

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;

0
投票

如果你在async/await的背景下,请使用@Cycloguy的答案。否则(例如在DI-Container中注册IScheduler):

ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = schedulerFactory.GetScheduler()
                                       .ConfigureAwait(false)
                                       .GetAwaiter()
                                       .GetResult();
© www.soinside.com 2019 - 2024. All rights reserved.