Nowadays, .NET Core is a widely popular framework. It has characteristics such as cross-platform, consistent across architectures, command-line tools, flexible deployment, compatible, open source, and it's supported by Microsoft.
Worker Services is a new template introduced by .NET Core 3. Now, we will discuss how to implement a Scheduler job by combining the .NET Core 3 Worker Service and Coravel.

What is Worker Service ?

Worker Service performs background work like email sending, SMS sending, event broadcasting, triggering some data with a specific time, triggering salary calculation, etc. It is a type of light-weight console application.
Let's understand with one example.
Prerequisite
Download .NET Core 3 SDK (current version is 3.0.0 preview 6) from the official Microsoft site.
.NET Core 3 - Building Backgorund Job Using Coravel Library

Create one empty folder

Run the command at this directory. Actually, it creates a sample console app with the use of worker template.
  1. dotnet new worker
.NET Core 3 - Building Backgorund Job Using Coravel Library
Let's open the project in Visual Studio. The progam.cs file looks like below.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Hosting;
  7. namespace CoreWorkerServiceSchedularJob
  8. {
  9. public class Program
  10. {
  11. public static void Main(string[] args)
  12. {
  13. CreateHostBuilder(args).Build().Run();
  14. }
  15. public static IHostBuilder CreateHostBuilder(string[] args) =>
  16. Host.CreateDefaultBuilder(args)
  17. .ConfigureServices((hostContext, services) =>
  18. {
  19. services.AddHostedService<Worker>();
  20. });
  21. }
  22. }

How to Configure Coravel with this template

Run the following command with NuGet Package console.
  1. dotnet add package coravel

Implement An Invocable

Let's add our own file (CoreWorkerServiceSchedularJob.cs) in which we write our custom text, such as "Works fine... my first schedule job.......!!!"
  1. using Coravel.Invocable;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace CoreWorkerServiceSchedularJob
  7. {
  8. class MyFirstJob : IInvocable
  9. {
  10. public Task Invoke()
  11. {
  12. Console.WriteLine("Works fine... my first schedule job.......!!!" + DateTime.Now.ToString());
  13. return Task.CompletedTask;
  14. }
  15. }
  16. }

Register Schedule Job And Specify Time

Now, let's register our job with .NET Core's service container and specify the time for five seconds. Below is the final code of Program.cs file.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Coravel;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Hosting;
  8. namespace CoreWorkerServiceSchedularJob
  9. {
  10. public class Program
  11. {
  12. public static void Main(string[] args)
  13. {
  14. IHost host = CreateHostBuilder(args).Build();
  15. host.Services.UseScheduler(scheduler => {
  16. // Remind schedular to repeat the same job in every five-second
  17. scheduler
  18. .Schedule<MyFirstJob>()
  19. .EveryFiveSeconds();
  20. });
  21. host.Run();
  22. }
  23. public static IHostBuilder CreateHostBuilder(string[] args) =>
  24. Host.CreateDefaultBuilder(args)
  25. .ConfigureServices(services =>
  26. {
  27. services.AddScheduler();
  28. // register job with container
  29. services.AddTransient<MyFirstJob>();
  30. });
  31. };
  32. }

It's time to run the application.

Let's look at the output of this application.
.NET Core 3 - Building Backgorund Job Using Coravel Library
It's time to wrap up this article. We implemented a Scheduler task in just 5 minutes of work with .NET Core 3 and a combination of Worker Service template and Coravel package.
Enjoy!