c# - quartz : Does not implement interface member

标签 c# quartz.net

我正在使用 Quartz 并使用示例代码并得到错误:

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.

这是我第一次使用 Quartz,如有任何帮助,我们将不胜感激。

public class EmailJob : IJob  // <<<--- Error on this line
{
    public void Execute(IJobExecutionContext context)
    {
        using (var message = new MailMessage("user@gmail.com", "user@live.co.uk"))
        {
            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("user@gmail.com", "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);
        }
    }

我直接从这篇精彩的文章中获得了代码:http://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net

最佳答案

在我看来您使用的是 3.0 版本(仔细检查您从 Nuget 获取的包)。 IJob 界面已更改。 Execute 方法现在返回一个 Task 而不是一个 void 方法(这解释了为什么你会看到你所看到的问题)。

任务执行( IJobExecutionContext 上下文 )

Here are the 3.0 docs .

正如 Bidou 所指出的,第 3 版仍处于 alpha 阶段。您需要卸载此版本并将其替换为以前的版本,或者相应地调整您的代码。

关于c# - quartz : Does not implement interface member,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41944213/

相关文章:

c# - 无法分配,因为它是方法组 C#?

azure - Azure 上的 Quartz.net 未找到时区 ID 'Coordinated Universal Time'

quartz.net - 在保留 "quartz_jobs.xml"文件的代码中创建作业

c# - Quartz.NET - 作业不运行?

c# - 用整数参数重载: Int16, Int32,Int64总是调用Int32?

c# - 具有相同类型的嵌套 Dto 的 Dto 失败

c# - 在 aspx 文件中使用内联代码设置文字文本

c# - System.Data.SqlClient.SqlException nvarchar 为 float

.net - 在 Quartz.NET 中,有没有办法设置一个只允许一个 Job 实例运行的属性?

c# - Quartz for asp.net 被 iis 服务器杀死