'How to get Quartz NET scheduler from services collection to CrystalQuartz
I'm trying to use Quartz NET and CrystalQuartz with .NET 5.
I add the scheduler to services collection with AddQuartz
How could I get the scheduler from the services collection within?
services.AddQuartz(q =>{
...
q.SchedulerId = "MyScheduler ID";
...
})
Solution 1:[1]
As a first step, you need to install CrystalQuartz.AspNetCore NuGet package to the target project.
As a next step, you need to hook CrystalQuartz middleware into your ASP.NET Core environment. It should be done by calling UseCrystalQuartz extension method at the moment of pipeline initialization. The generic syntax for panel configuration looks like this:
app.UseCrystalQuartz(() => scheduler, options);
scheduler is your IScheduler (local or remote).
You should already have an IScheduler object instance so you can pass a Func pointing to it to the configuration extension method:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// ...
IScheduler scheduler = CreateScheduler();
app.UseCrystalQuartz(() => scheduler);
// ...
}
// this method is just a sample of scheduler initialization
private IScheduler CreateScheduler()
{
var schedulerFactory = new StdSchedulerFactory();
var scheduler = schedulerFactory.GetScheduler().Result;
// construct job info
var jobDetail = JobBuilder.Create<HelloJob>()
.WithIdentity("myJob")
.StoreDurably()
.Build();
// fire every minute
var trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger")
.StartNow()
.WithSimpleSchedule(x => x.WithIntervalInMinutes(1).RepeatForever())
.Build();
scheduler.ScheduleJob(jobDetail, trigger);
scheduler.Start();
return scheduler;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Qing Guo |
