Quartz with Asp.Net Core 3.1

2.4k Views Asked by At

I am working on ASP.NET 3.1 Web Api project and I want to configure Quartz to schedule jobs which will work after 10 seconds. I need help in this because I setup quartz but job is not firing. I installed 2 NuGet's

  1. Quartz.AspNetCore (3.1.0)

  2. Quartz.Extensions.DependencyInjection (3.1.0)

    public void ConfigureServices(IServiceCollection services) {

            services.AddQuartz(q =>
            {
                q.SchedulerId = "Scheduler-Core";
    
                q.UseMicrosoftDependencyInjectionJobFactory(options =>
                {
                    // if we don't have the job in DI, allow fallback to configure via default constructor
                    options.AllowDefaultConstructor = true;
                });
    
                // these are the defaults
                q.UseSimpleTypeLoader();
                q.UseInMemoryStore();
                q.UseDefaultThreadPool(tp =>
                {
                    tp.MaxConcurrency = 10;
                });
    
    
                var jobKey = new JobKey("SchedulerJob", "SchedulerJob");
                q.AddJob<SchedulerJob>(j => j
                    .WithIdentity(jobKey)
                    .WithDescription("SchedulerJob")
                );
    
                q.AddTrigger(t => t
                        .WithIdentity("SchedulerJob Trigger")
                        .ForJob(jobKey)
                        .StartNow()
                        .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(10)).RepeatForever())
                        .WithDescription("SchedulerJob simple trigger")
                    );
    
                // base quartz scheduler, job and trigger configuration
            });
    
            // ASP.NET Core hosting
            services.AddQuartzServer(options =>
            {
                // when shutting down we want jobs to complete gracefully
                options.WaitForJobsToComplete = true;
            });
    
            services.AddControllersWithViews().AddNewtonsoftJson();
        }
    

Here is the job which i want to execute after evert 10 seconds.

public class SchedulerJob : IJob
    {
        private readonly ISendNotificationService _sendNotificationService;

        public SchedulerJob(ISendNotificationService sendNotificationService)
        {
            _sendNotificationService = sendNotificationService;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                await _sendNotificationService.SendScheduledNotifications();
            }
            catch (Exception ex)
            {
            }
        }
    }
0

There are 0 best solutions below