Task Scheduler in Asp.Net core
Welcome, fellow developers, to Code2Night! In this article, we will see a task scheduler in asp.net core. In the realm of building robust web applications with ASP.NET Core, there comes a time when we need to automate certain processes or tasks to run at regular intervals. Whether it's sending out email reminders, updating database records, or performing background computations, a reliable task scheduler becomes an indispensable tool in our arsenal.
In this blog post, we will delve into the exciting world of task scheduling in ASP.NET Core. We'll explore how to design and implement a task to execute recurring jobs efficiently, ensuring that critical processes are automated with precision.
Throughout this journey, we'll harness the power of ASP DOT NET Core, a versatile and powerful framework that has revolutionized web development. We'll leverage its flexibility, performance, and extensive ecosystem to create a task that fits seamlessly into our application architecture.
By the end of this blog post, you'll have a comprehensive understanding of how to build a robust and reliable task for recurring jobs in your ASP DOT NET Core applications. You'll be equipped with the knowledge and tools to automate your application's processes effectively, allowing you to focus on what truly matters—delivering exceptional user experiences.
So, let's embark on this coding adventure together and discover the art of task scheduling in ASP DOT NET Core. Are you ready? Let's dive in!
Task schedulers are basically used to run some piece of code at regular intervals so they can be called recurring jobs. So for creating a task scheduler in asp.net core follow these steps
So, first of all, we have to take a new asp.net core project application and add one new class SchedulerService.
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Threading; using System.Threading.Tasks; namespace SchedulerService.Models { public class SchedulerService : IHostedService, IDisposable { private int executionCount = 0; private System.Threading.Timer _timerNotification; public IConfiguration _iconfiguration; private readonly IServiceScopeFactory _serviceScopeFactory; private readonly Microsoft.AspNetCore.Hosting.IHostingEnvironment _env; public SchedulerService(IServiceScopeFactory serviceScopeFactory, Microsoft.AspNetCore.Hosting.IHostingEnvironment env, IConfiguration iconfiguration) { _serviceScopeFactory = serviceScopeFactory; _env = env; _iconfiguration = iconfiguration; } public Task StartAsync(CancellationToken stoppingToken) { _timerNotification = new Timer(RunJob, null, TimeSpan.Zero, TimeSpan.FromMinutes(1)); /*Set Interval time here*/ return Task.CompletedTask; } private void RunJob(object state) { using (var scrope = _serviceScopeFactory.CreateScope()) { try { // var store = scrope.ServiceProvider.GetService<IStoreRepo>(); /* You can access any interface or service like this here*/ //store.GetAll(); /* You can access any interface or service method like this here*/ /* Place your code here which you want to schedule on regular intervals */ } catch (Exception ex) { } Interlocked.Increment(ref executionCount); } } public Task StopAsync(CancellationToken stoppingToken) { _timerNotification?.Change(Timeout.Infinite, 0); return Task.CompletedTask; } public void Dispose() { _timerNotification?.Dispose(); } } }
In the RunJob you can write the code that you want to execute at regular intervals. You can change the time interval according to your requirement. Now you have to add the service in startup.cs inside ConfigureServices method.
services.AddHostedService<SchedulerService.Models.SchedulerService>();
Now run the application and the RunJob method will run after the given interval. This is how you can create a task scheduler in asp.net core.