Task Scheduler in ASP.NET Core | Code2Night
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

Task Scheduler in Asp.Net core

Date- Jul 20,2022

14443

Free Download Pay & Download
Scheduler Service Task Scheduler

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 scheduler

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.

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1190
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts