简体   繁体   中英

Quartz.Net don't works. Bad Code. I already tried to place the IJob call in this way: Quartz.IJob, but it does not work either

  1. List item

I am trying to implement Quartz.Net in my project, I have placed two .cs where I declare public classes, however, the code that I incorporate, gives me errors, which I detail below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using Quartz.Listener;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;

namespace WEB_Project.Models
{
    public class EmailJob : IJob /*Error 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);
                }
            }
        }
    }
}
  • In this line, the error appears: 在此处输入图片说明 I already tried to place the IJob call in this way: Quartz.IJob, but it does not work either.

In the other .cs, I have the JOB, but it also gives me an error, I explain it below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using Quartz.Impl;
using System.Threading.Tasks;

namespace WEB_Project.Models
{
    public class JobScheduler
    {
        public void Start(DateTime date)
        {
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); /*Error Line*/
            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);
        }
    }
}
  • In this line, the error appears: 在此处输入图片说明 These are the mistakes that the implementation of Quartz gives me, any idea of how to fix it?

Finally I could solve the problem, thanks to those who helped me and others, thank you very much for nothing ... I leave the code, so that anyone who has the same mistakes I had, know how to fix it and do not confuse it with a link that be taken as a duplicate ...

First Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using Quartz.Listener;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;

namespace WEB_Project.Models
{
    public class EmailJob : IJob 
    {
        public Task 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);
                }
            }
          return Task.CompletedTask;
        }
    }
}

Second Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using Quartz.Impl;
using System.Net;
using System.Net.Mail;
using Quartz.Logging;
using System.Threading.Tasks;

namespace WEB_Project.Models
{
    public class JobScheduler
    {
        public static async void Start()
        {
            IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler();
            await scheduler.Start();

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

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

            ITrigger trigger1 = TriggerBuilder.Create()
                .WithIdentity("trigger_1", "group_1")
                .StartNow()
                .WithSimpleSchedule(x => x
                    .WithIntervalInSeconds(20)
                    .RepeatForever())
                .Build();

            await scheduler.ScheduleJob(job, trigger1);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM