Quartz: Does not implement interface member
Asked Answered
M

4

5

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

Multure answered 30/1, 2017 at 19:46 Comment(0)
B
2

I just tested your code and it compiles without any changes on my side. Your problem is maybe a wrong namespace import. You can try with the full namespace like this:

public class EmailJob : Quartz.IJob
{
    public void Execute(Quartz.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);
            }
        }
    }

   // ...
}
Barris answered 30/1, 2017 at 20:22 Comment(2)
Thank you for your help. I am still getting the error. What are your usings and what version of Quartz are you using?Multure
I switched to 2.4.1 and used your code and it worked.Multure
L
7

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.

Lugar answered 30/1, 2017 at 20:26 Comment(2)
Note that version 3.0 is still in Alpha 2 !Barris
@Bidou True, I'll note that in my answer. OP should probably uninstall that version and install a previous version consistent with the tutorial referenced.Lugar
B
2

I just tested your code and it compiles without any changes on my side. Your problem is maybe a wrong namespace import. You can try with the full namespace like this:

public class EmailJob : Quartz.IJob
{
    public void Execute(Quartz.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);
            }
        }
    }

   // ...
}
Barris answered 30/1, 2017 at 20:22 Comment(2)
Thank you for your help. I am still getting the error. What are your usings and what version of Quartz are you using?Multure
I switched to 2.4.1 and used your code and it worked.Multure
S
1

I had the same error.

Fixed it by running Install-Package Quartz -Version 3.0.0-alpha1 -Pre in the Package Manager Console.

Sublease answered 29/3, 2017 at 13:9 Comment(0)
A
1

Run this line in your Package Manager to install the appropriate package: Install-Package Quartz -Version 2.2.4

Amplification answered 21/2, 2022 at 8:20 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Arand

© 2022 - 2024 — McMap. All rights reserved.