.NET Core 3 - Building Background Job Using Coravel Library

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.   
  8. namespace CoreWorkerServiceSchedularJob  
  9. {  
  10.     public class Program  
  11.     {  
  12.         public static void Main(string[] args)  
  13.         {  
  14.             CreateHostBuilder(args).Build().Run();  
  15.         }  
  16.   
  17.         public static IHostBuilder CreateHostBuilder(string[] args) =>  
  18.             Host.CreateDefaultBuilder(args)  
  19.                 .ConfigureServices((hostContext, services) =>  
  20.                 {  
  21.                     services.AddHostedService<Worker>();  
  22.                 });  
  23.     }  
  24. }  

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.   
  7. namespace CoreWorkerServiceSchedularJob  
  8. {  
  9.     class MyFirstJob  : IInvocable  
  10.     {  
  11.         public Task Invoke()  
  12.         {  
  13.             Console.WriteLine("Works fine... my first schedule job.......!!!" + DateTime.Now.ToString());  
  14.             return Task.CompletedTask;  
  15.         }  
  16.     }  
  17. }  

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.   
  9. namespace CoreWorkerServiceSchedularJob  
  10. {  
  11.     public class Program  
  12.     {  
  13.         public static void Main(string[] args)  
  14.         {  
  15.             IHost host = CreateHostBuilder(args).Build();  
  16.             host.Services.UseScheduler(scheduler => {  
  17.                 
  18.                 // Remind schedular to repeat the same job in every five-second   
  19.                 scheduler  
  20.                     .Schedule<MyFirstJob>()  
  21.                     .EveryFiveSeconds();  
  22.   
  23.   
  24.             });  
  25.             host.Run();  
  26.         }  
  27.   
  28.         public static IHostBuilder CreateHostBuilder(string[] args) =>  
  29.             Host.CreateDefaultBuilder(args)  
  30.                 .ConfigureServices(services =>  
  31.                 {  
  32.                     services.AddScheduler();  
  33.   
  34.                     // register job with container  
  35.                     services.AddTransient<MyFirstJob>();  
  36.                 });  
  37.     };  
  38. }  

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! 


Similar Articles