guryanovev / CrystalQuartz

pluggable UI for Quartz.NET
MIT License
1.01k stars 313 forks source link

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

Open ytcitsupport-jlin opened 2 years ago

ytcitsupport-jlin commented 2 years ago

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

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?

Lexy2 commented 2 years ago

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)));

lucapisano commented 2 years ago

@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

Lexy2 commented 2 years ago

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

lucapisano commented 2 years ago

@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) 😃

Lexy2 commented 2 years ago

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

jasonlaw commented 8 months ago

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.