应用程序启动时 ASP.NET Core Quartz 错误:未设置数据源名称

问题描述 投票:0回答:1

我正在尝试在 ASP.NET Core 8.0 项目中配置并启动 Quartz。在这个项目中我这样配置 Quartz:

public static class QuartzConfiguration
{
    public static void AddQuartzConfiguration(this WebApplicationBuilder builder)
    {
        var serviceProvider = builder.Services.BuildServiceProvider();
        
        var notificationCronExpression = serviceProvider.GetService<IOptions<NotificationCronJobExpressionSettings>>();
        var checkTaskStatusCronExpression = serviceProvider.GetService<IOptions<CheckStatusIntervalExpressionSettings>>();
        var legislativeCronExpression = serviceProvider.GetService<IOptions<LegislativeUpdatesRefreshCronExpressionSettings>>();
        var payRunTasksToAutoCompleteCronExpression = serviceProvider.GetService<IOptions<PayRunTYDmcVEzKLbAqWEQ4iXCPYVshvCf5tW9zAtings>>();

        builder.Services.AddQuartz(quartzConfigurator =>
        {
            quartzConfigurator.UseJobAutoInterrupt(options => options.DefaultMaxRunTime = TimeSpan.FromMinutes(10));
            quartzConfigurator.UseDefaultThreadPool(maxConcurrency: 1);
            quartzConfigurator.UseTimeZoneConverter();
            quartzConfigurator.UsePersistentStore(store =>
            {
                store.UseNewtonsoftJsonSerializer();
            });
            
            quartzConfigurator.AddQuartzJob<GenerateTaskStatusPollerFactory>(checkTaskStatusCronExpression.Value.Value, "CheckTaskStatus", "Test", "CheckTaskStatusTrigger", "GenerateTaskStatusPollerFactory");
            quartzConfigurator.AddQuartzJob<GenerateNotificationsPollerFactory>(notificationCronExpression.Value.Value, "GenerateNotifications", "Test", "GenerateNotificationsTrigger", "GenerateNotificationsPollerFactory");
            quartzConfigurator.AddQuartzJob<LegislativeUpdatePollerFactory>(legislativeCronExpression.Value.Value, "PollLegislativeUpdates", "Test", "PollLegislativeUpdatesTrigger", "LegislativeUpdatePollerFactory");
            quartzConfigurator.AddQuartzJob<PayRunTaskStatusPollerFactory>(payRunTasksToAutoCompleteCronExpression.Value.Value, "AutoCompletePayRunTasks", "Test", "AutoCompletePayRunTasksTrigger", "PayRunTaskStatusPollerFactory");
        });

        builder.Services.AddQuartzServer(options =>
        {
            // when shutting down we want jobs to complete gracefully
            options.WaitForJobsToComplete = true;
        });
    }

为了简化添加作业,我创建了一个扩展方法:

public static class QuartzConfiguratorExtensions
{
    public static void AddQuartzJob<T>(this IServiceCollectionQuartzConfigurator quartzConfigurator,
                                      string cronExpression,
                                      string key, string group,
                                      string triggerName, string triggerGroup) where T: IJob
    {
        bool isCronExpressionValid = CronExpression.IsValidExpression(cronExpression);

        if(!isCronExpressionValid)
        {
            throw new Exception ($"{cronExpression} is not a valid cron expression for Quartz job {key}");
        }

        quartzConfigurator.AddJob<T>(jobConfigurator => jobConfigurator.WithIdentity(key, group));

        quartzConfigurator.AddTrigger(triggerConfigurator => triggerConfigurator
                          .ForJob(key)
                          .WithIdentity(triggerName, triggerGroup)
                          .WithCronSchedule(cronExpression));
    }
}

当我启动我的应用程序时,我在

Program.cs

处收到错误
app.Run();

错误提示:

未设置数据源名称

堆栈跟踪:

at   Quartz.Impl.AdoJobStore.JobStoreSupport.<Initialize>d__143.MoveNext()
at Quartz.Impl.AdoJobStore.JobStoreTX.<Initialize>d__0.MoveNext()
at Quartz.Impl.StdSchedulerFactory.<Instantiate>d__67.MoveNext()
at Quartz.Impl.StdSchedulerFactory.<Instantiate>d__67.MoveNext()
at Quartz.Impl.StdSchedulerFactory.<GetScheduler>d__73.MoveNext()
at Quartz.ServiceCollectionSchedulerFactory.<GetScheduler>d__6.MoveNext()
at Quartz.QuartzHostedService.<StartAsync>d__7.MoveNext()
at Microsoft.Extensions.Hosting.Internal.Host.<<StartAsync>b__15_1>d.MoveNext()
at Microsoft.Extensions.Hosting.Internal.Host.<ForeachService>d__18`1.MoveNext()
at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__15.MoveNext()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
at Program.<Main>$(String[] args) in C:\_PROJECTS_\PPM\IIPay.PPM.Api\Program.cs:line 62

我尝试阅读各种内容,但没有找到解决方案。

谢谢

c# asp.net-core quartz-scheduler quartz.net quartz
1个回答
0
投票

所以我找到了设置RAMJobStore的方法。即使文档说这是默认存储,但事实并非如此。 像下面这样设置商店后,它就开始工作了。

store.SetProperty("quartz.jobStore.type", "Quartz.Simpl.RAMJobStore, Quartz");

如果您想使用某些数据库作为存储,您可以像下面这样定义它(例如 MS SQL 服务器):

store.UseSqlServer(coonectionString);
store.PerformSchemaValidation = true;
© www.soinside.com 2019 - 2024. All rights reserved.