我正在使用 Quartz 并使用示例代码并收到错误:
CS0738“EmailJob”未实现接口成员IJob.Execute(IJobExecutionContext)
. EmailJob.Execute(IJobExecutionContext)
无法实施IJob.Execute(IJobExecutionContext)
因为它没有>具有匹配的返回类型Task
.
这是我第一次接触 Quartz,因此我们将不胜感激。
public class EmailJob : IJob // <<<--- Error on this line
{
public void Execute(IJobExecutionContext context)
{
using (var message = new MailMessage("[email protected] /cdn-cgi/l/email-protection", "[email protected] /cdn-cgi/l/email-protection"))
{
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] /cdn-cgi/l/email-protection", "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 http://www.mikesdotnetting.com/article/254/scheduled-tasks-in-asp-net-with-quartz-net
在我看来,您正在使用 3.0 版本(仔细检查您从 Nuget 获取的软件包)。 IJob 界面已更改。 Execute 方法现在返回一个任务,而不是一个 void 方法(这解释了为什么您会看到您所看到的问题)。
Task Execute(
IJobExecutionContext context
)
这是 3.0 文档 http://quartznet.sourceforge.net/apidoc/3.0/html/.
正如 Bidou 所指出的,版本 3 仍处于 alpha 阶段。您需要卸载此版本并将其替换为以前的版本,或相应地调整您的代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)