Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz: Does not implement interface member

Tags:

c#

quartz.net

I am using Quartz and using sample code and get the error:

CS0738 'EmailJob' does not implement interface member IJob.Execute(IJobExecutionContext). EmailJob.Execute(IJobExecutionContext) cannot implement IJob.Execute(IJobExecutionContext) because it does not > have the matching return type of Task.

This is my first go at Quartz so any help would be kindly appreciated.

public class EmailJob : IJob  // <<<--- Error on this line
{
    public void Execute(IJobExecutionContext context)
    {
        using (var message = new MailMessage("[email protected]", "[email protected]"))
        {
            message.Subject = "Test";
            message.Body = "Test at " + DateTime.Now;
            using (SmtpClient client = new SmtpClient
            {
                EnableSsl = true,
                Host = "smtp.gmail.com",
                Port = 587,
                Credentials = new NetworkCredential("[email protected]", "password")
            })
            {
                client.Send(message);
            }
        }
    }

 public class JobScheduler
    {
        public static void Start()
        {
            IScheduler scheduler = (IScheduler)StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();

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

            ITrigger trigger = TriggerBuilder.Create()
                .WithDailyTimeIntervalSchedule
                  (s =>
                     s.WithIntervalInHours(24)
                    .OnEveryDay()
                    .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 0))
                  )
                .Build();

            scheduler.ScheduleJob(job, trigger);
        }
    }

I got the code directly from this wonderful article: http://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net

like image 739
Missy Avatar asked Feb 06 '23 05:02

Missy


1 Answers

It looks to me like you're using the 3.0 version (double check which package you grabbed from Nuget). The IJob interface has changed. The Execute method now returns a Task instead of being a void method (which explains why you're seeing the issue you're seeing).

Task Execute( IJobExecutionContext context )

Here are the 3.0 docs.

As noted by Bidou, version 3 is still in alpha. You need to uninstall this version and replace it with a previous version, or adjust your code accordingly.

like image 141
Matt M Avatar answered Feb 08 '23 16:02

Matt M