CrystalQuartz icon indicating copy to clipboard operation
CrystalQuartz copied to clipboard

An error occurred while initialization of scheduler services An instance of Quartz 3 Scheduler expected

Open ytcitsupport-jlin opened this issue 2 years ago • 6 comments

I'm trying to use CrystalQuartz with my ASP.NET Core application. Environment:

  • Framework: .NET 5.0
  • Quartz.AspNetCore: 3.4.0
  • CrystalQuartz.AspNetCore: 6.8.1

This is my setup in the Startup.cs

public void ConfigureServices(IServiceCollection services){
...
services.AddSingleton<IJobFactory, SingletonJobFactory>();
            services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
            var schedulerFactory = new StdSchedulerFactory();
            var scheduler = schedulerFactory.GetScheduler().Result;
            services.AddSingleton<IScheduler>(scheduler);

            // Add our job
            services.AddSingleton<HelloJob>();
            services.AddSingleton(new JobSchedule(
                jobType: typeof(HelloJob),
                cronExpression: "0/5 * * * * ?")); // run every 5 seconds

            services.AddHostedService<QuartzHostedService>();
...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
...
app.UseCrystalQuartz(() => Scheduler);
...
}

But the scheduler runs OK but when I open CrystalQuartz on the browser, I saw the error message

An error occurred while initialization of scheduler services An instance of Quartz 3 Scheduler expected

Did I set it up wrong?

ytcitsupport-jlin avatar Apr 22 '22 21:04 ytcitsupport-jlin

app.UseCrystalQuartz(() => Scheduler);

I don't see where you define Scheduler variable or property that you pass to CrystalQuartz. You would need to build the app and pass the service to it, something like builder.UseCrystalQuartz(app.Services.GetService(typeof(IScheduler)));

Lexy2 avatar Jun 29 '22 07:06 Lexy2

@ytcitsupport-jlin replace "var scheduler = schedulerFactory.GetScheduler().Result;" with "var scheduler = schedulerFactory.GetScheduler().GetAwaiter().GetResult();" and it will work. You're not awaiting the async method

lucapisano avatar Aug 22 '22 15:08 lucapisano

@lucapisano and neither are you. You're blocking on async method.

Lexy2 avatar Aug 22 '22 20:08 Lexy2

@lucapisano and neither are you. You're blocking on async method.

The GetAwaiter() method will block the current thread until the async method returns its result and then it gives it back to the caller. Please note that this is a workaround, a nicer approach I might suggest is to change the UseCrystalQuartz method to accept as input a lambda function that let's you choose the scheduler (given the factory as lambda input) 😃

lucapisano avatar Aug 22 '22 20:08 lucapisano

The only difference between .GetAwaiter().GetResult() and .Result is that the first one unwraps any exceptions that might occur, and .Result returns an AggregateException.

Lexy2 avatar Aug 22 '22 21:08 Lexy2

I'm trying to use CrystalQuartz with my ASP.NET Core application. Environment:

  • Framework: .NET 5.0
  • Quartz.AspNetCore: 3.4.0
  • CrystalQuartz.AspNetCore: 6.8.1

This is my setup in the Startup.cs

public void ConfigureServices(IServiceCollection services){
...
services.AddSingleton<IJobFactory, SingletonJobFactory>();
            services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
            var schedulerFactory = new StdSchedulerFactory();
            var scheduler = schedulerFactory.GetScheduler().Result;
            services.AddSingleton<IScheduler>(scheduler);

            // Add our job
            services.AddSingleton<HelloJob>();
            services.AddSingleton(new JobSchedule(
                jobType: typeof(HelloJob),
                cronExpression: "0/5 * * * * ?")); // run every 5 seconds

            services.AddHostedService<QuartzHostedService>();
...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
...
app.UseCrystalQuartz(() => Scheduler);
...
}

But the scheduler runs OK but when I open CrystalQuartz on the browser, I saw the error message

An error occurred while initialization of scheduler services An instance of Quartz 3 Scheduler expected

Did I set it up wrong?

Had you resolved the issue? I am having same problem too.

jasonlaw avatar Feb 22 '24 09:02 jasonlaw