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.

Create one empty folder

Run the command at this directory. Actually, it creates a sample console app with the use of worker template.

dotnet new worker

Let's open the project in Visual Studio. The progam.cs file looks like below.

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace CoreWorkerServiceSchedularJob { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddHostedService<Worker>(); }); } }

How to Configure Coravel with this template

Run the following command with NuGet Package console.

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.......!!!"

using Coravel.Invocable; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace CoreWorkerServiceSchedularJob { class MyFirstJob : IInvocable { public Task Invoke() { Console.WriteLine( "Works fine... my first schedule job.......!!!" + DateTime.Now.ToString()); return Task.CompletedTask; } } }

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.

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Coravel; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace CoreWorkerServiceSchedularJob { public class Program { public static void Main(string[] args) { IHost host = CreateHostBuilder(args).Build(); host.Services.UseScheduler(scheduler => { scheduler .Schedule<MyFirstJob>() .EveryFiveSeconds(); }); host.Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices(services => { services.AddScheduler(); services.AddTransient<MyFirstJob>(); }); }; }

It's time to run the application.

Let's look at the output of this application.

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!