What is Quartz.NET?
Quartz.NET is an open-source framework for scheduling various tasks. It can be integrated into any application created on the .NET platform, ranging from the smallest services to large-scale enterprise systems. The framework operates on two main concepts: a task that runs on a schedule in the background and a scheduler that is responsible for executing the task based on a trigger.Creating a Project
First, let's create a project running on the .NET 6 platform. In this case, we will utilize the .NET Core reporting tool, which is a full-featured, universal, and cross-platform tool for creating, processing, building, displaying, and converting reports and dashboards. It can be easily integrated into a custom application by installing the appropriate NuGet package.Installing packages
So, since this example will not use visual components (viewer and report designer), to export a report, it is sufficient to install the NuGet package - Stimulsoft.Reports.Engine.NetCore.Additionally, to add and use the scheduler, you must install the Quartz package.
After successfully installing the packages, the project setup is complete, and you can proceed to defining tasks and schedules. Note!
To work with dashboards, you will need to install the Stimulsoft.Dashboards.Web.NetCore package.
Defining tasks
Let's create a new class, StiReportJob.cs, which should contain an implementation of the IJob interface with a definition of the Execute() method. We will also add two methods: one for creating (or loading) a report and another for exporting the report and sending it by email. Thus, the Execute() method will call the report creation method and then invoke the asynchronous email-sending method. Below is the code for the StiReportJob.cs class.StiReportsJob.cs
public class StiReportJob : IJob
{
public async Task Execute(IJobExecutionContext context)
{
var report = CreateReport();
await SendEmail(report);
}
private StiReport CreateReport()
{
var report = new StiReport();
// report.Load();
return report;
}
private async Task SendEmail(StiReport report)
{
using (MailMessage mail = new MailMessage("This email address is being protected from spambots. You need JavaScript enabled to view it. ", "This email address is being protected from spambots. You need JavaScript enabled to view it. "))
{
mail.Subject = "New Report";
mail.Body = "Body Info";
using var stream = new MemoryStream();
report.ExportDocument(StiExportFormat.Pdf, stream);
stream.Position = 0;
var attachment = new Attachment(stream, "report.pdf", "application/pdf");
mail.Attachments.Add(attachment);
using var client = new SmtpClient
{
EnableSsl = true,
Host = "host.com",
Port = 25,
};
await client.SendMailAsync(mail);
}
}
}
Despite the fact that we have considered the basic case of working with a report, much more complex and creative tasks can be implemented here.
Setting up a schedule
Let's create another new class - StiJobScheduler.cs, in which we will initialize the scheduler and add a trigger to it. The trigger, in this case, is the condition that starts the scheduler. In the example below, the trigger is set for the time between 10:00 AM and noon every day. However, there can be a wide variety of triggers, ranging from recurring schedules to an endless cycle of triggering the scheduler every minute. Below is the code for the StiJobScheduler.cs class.StiJobScheduler.cs
public class StiJobScheduler
{
public static async void Start()
{
var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
await scheduler.Start();
var job = JobBuilder.Create<StiReportJob>().Build();
var trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.WithCronSchedule($"0 0 10 ? * *")//At 10:00 AM
.Build();
await scheduler.ScheduleJob(job, trigger);
}
}
After this, in the main application class, in this example, Program.cs, it is necessary to call the static Start() method of the StiJobScheduler.cs class.
Program.cs
StiJobScheduler.Start();
Thus, today we demonstrated an example of automating the process, and now, every day at 10:00, the report will be converted into a PDF file, and a letter with it will be sent to the specified person.
We have also prepared a detailed video about the work with schedulers in Quartz.NET framework in our products.
If you have got any queries, feel free to get in touch with us!