Quartz.NET error comes every time I use it

2019-07-14 19:41发布

I get an error in this line saying:

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'Quartz.IScheduler'. An explicit conversion exists (are you missing a cast?)

How to solve it; I don't understand? Please help!

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();
    }
}

4条回答
闹够了就滚
2楼-- · 2019-07-14 20:17

From version 3.0.0 Quartz: https://www.quartz-scheduler.net/2017/12/30/quartznet-3.0-released.html :

  • SimpleThreadPool is gone, old owned threads are gone

  • IJob interface now returns a task

So I put here the example to use:

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楼-- · 2019-07-14 20:17

I ran into the same issue. When I loaded quartz from Manage NuGet Packages it gave me Version: 3.0.0-alpha2 (Prerelease). This caused the error that you are currently seeing. I found an earlier version at https://www.nuget.org/packages/Quartz/2.3.3 followed the instructions to install it, rebuilt my program and it worked like it should.

查看更多
仙女界的扛把子
4楼-- · 2019-07-14 20:23

If you're in a async/await context, use @Cycloguy's answer. Else (e.g. registering IScheduler in a DI-Container):

ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = schedulerFactory.GetScheduler()
                                       .ConfigureAwait(false)
                                       .GetAwaiter()
                                       .GetResult();
查看更多
来,给爷笑一个
5楼-- · 2019-07-14 20:25

I can't explain perfectly, but I know how it is working.

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

You need to get the result of the GetDefaultScheduler()so it looks like:

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
查看更多
登录 后发表回答