Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ytcitsupport-jlin opened this issue Apr 22, 2022 · 6 comments

Comments

@ytcitsupport-jlin
Copy link

ytcitsupport-jlin commented Apr 22, 2022

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?

@Lexy2
Copy link

Lexy2 commented Jun 29, 2022

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
Copy link

@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
Copy link

Lexy2 commented Aug 22, 2022

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

@lucapisano
Copy link

@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
Copy link

Lexy2 commented Aug 22, 2022

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants