Quartz.NET error comes every time I use it
Asked Answered
S

5

5

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();
    }
}
Snooze answered 5/11, 2016 at 11:1 Comment(2)
did you tried: ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler = schedulerFactory.GetScheduler();Ludivinaludlew
Is it possible that you have another class called StdSchedulerFactory which has a method called GetDefaultScheduler which returns a Task? If so try IScheduler scheduler = Quartz.Impl.StdSchedulerFactoryy.GetDefaultScheduler();Diphthongize
S
12

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;
    }
}
Skirling answered 4/5, 2018 at 2:44 Comment(0)
C
5

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;
Carmelitacarmelite answered 24/6, 2018 at 11:36 Comment(0)
A
3

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.

Annual answered 5/1, 2017 at 19:0 Comment(0)
B
0

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();
Baking answered 20/11, 2018 at 10:47 Comment(0)
C
0

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.
VS2007 Nuget Package Manager for Quartz

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.

enter image description here

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.

Crosley answered 8/11, 2019 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.