2017-01-30 71 views
2

我使用石英和使用的示例代码并且得到错误:石英:不实现接口成员

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 .

这是我第一次石英走得那么任何帮助,将好心赞赏。

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); 
     } 
    } 

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

回答

1

我只是测试你的代码,它编译没有在我身边的任何变化。你的问题可能是一个错误的命名空间导入。您可以使用完整的命名空间这样的尝试:

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); 
      } 
     } 
    } 

    // ... 
} 
+0

谢谢你的帮助。我仍然收到错误。你使用什么和你使用什么版本的Quartz? – Missy

+0

我切换到2.4.1并使用您的代码,它的工作。 – Missy

3

它看起来像你对我正在使用的版本3.0(您的NuGet抓起哪个包仔细检查)。 IJob界面已更改。 Execute方法现在返回一个Task,而不是一个无效的方法(这就解释了为什么你看到了你所看到的问题)。

Task Execute( IJobExecutionContext context )

Here are the 3.0 docs

正如Bidou所说,版本3仍然是阿尔法。您需要卸载此版本并将其替换为以前的版本,或者相应地调整您的代码。

+1

请注意,版本3.0仍在Alpha 2中! – Bidou

+1

@Bidou真的,我会在我的答案中注意到。 OP应该可能卸载该版本并安装与所引用的教程一致的先前版本。 –

1

我有同样的错误。

通过在包管理器控制台中运行Install-Package Quartz -Version 3.0.0-alpha1 -Pre来修复它。

相关问题