Quartz.NET error comes every time I use it

二次信任 提交于 2019-12-11 03:04:17

问题


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

回答1:


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



回答2:


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.




回答3:


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;



回答4:


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:


One additional note to the answers so far.

As mentioned by Cycloguy, many 'breaking' changes occurred with Quartz 3.0.0. As of 11/8/19 in VisualStudio 2017, 3.0.7 is the current version of Quartz.

Alternate solution: Try installing a version of Quartz prior to 3.0.0 (e.g. 2.4.1). Not a great long term solution, but a great bandaid if you're in a hurry.

Example. Quartz Version 2.4.1 works fine with .NET 4.6.2 in VS2017. (And avoids this error.)

I'll be installing 3.0.7 soon. But, I need this bandaid to hobble along temporarily.

Addendum: VS2007 Nuget Package Manager does an odd thing in its display. It's technically 'ok', but leads directly to this error.

If you're looking at the installed items, the right-hand side shows the current version... NOT the version you have installed.

In the previous image, the version installed is 2.4.1. So as a developer, if you're looking at older code, it's easy (when doing a quick scan) to think that old code is using version 3.0.7. However, do not use the version # in the 'installed' rows to determine your version number; the box slightly to the right that shows the actual version implemented.

And it's this (using >=3.0.0 when you really wanted <3.0.0, at least temporarily) that easily leads to the error which is discussed on this page.



来源:https://stackoverflow.com/questions/40437430/quartz-net-error-comes-every-time-i-use-it

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