0

我正在构建一个 Orchard CMS 网站项目,我需要安排一些工作,这些数据经常存储在数据库中,所以我在 Orchard.Web 的 Global.asax 中使用 Quartz.NET,如下所示:

protected void Application_Start() {
    RegisterRoutes(RouteTable.Routes);
    _starter = new Starter<IOrchardHost>(HostInitialization, HostBeginRequest, HostEndRequest);
    _starter.OnApplicationStart(this);

    ISchedulerFactory sf = new StdSchedulerFactory();

    // get a scheduler
    IScheduler sched = sf.GetScheduler();
    sched.Start();

    var job = JobBuilder.Create<JobWorker>()
    .WithIdentity("job1", "group1")
    .Build();

    ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("trigger1", "group1")
    .StartAt(DateTime.Now)
    .WithCronSchedule("5 0/1 * * * ?")
    .Build();

    sched.ScheduleJob(job, trigger);
}

并且 JobWorker 类 - 与 Orchard.Web 中的 Global.asax 放置在同一级别的文件夹中:

public class JobWorker : IJob, IDependency {
    private readonly ISchedulerService _schedulerService;

    public JobWorker (ISchedulerService schedulerService) {
        _schedulerService = schedulerService;
    }

    public void Execute(IJobExecutionContext context) {
        _schedulerService.ExecuteJob();

    }
}

但是,我在调试输出控制台中收到了如下结果:

A first chance exception of type 'System.ArgumentException' occurred in Quartz.dll
A first chance exception of type 'Quartz.SchedulerException' occurred in Quartz.dll
A first chance exception of type 'Quartz.SchedulerException' occurred in Quartz.dll
The thread '<No Name>' (0x2278) has exited with code 0 (0x0).
The thread '<No Name>' (0x3368) has exited with code 0 (0x0).
The thread '<No Name>' (0x22a8) has exited with code 0 (0x0).
The thread '<No Name>' (0x2bc8) has exited with code 0 (0x0).

我已经尝试在 web mvc 4 项目中使用此代码 - 而不是果园 - 它工作正常。因此,我认为问题出在 Orchard CMS 上。我应该怎么办 ?我只需要一个计时器来循环调用 SchedulerService 中的方法 ExecuteJob() !

4

1 回答 1

3

我不确定如何让 Quartz.NET 在 Orchard 中工作,但 Orchard 有自己的服务来处理重复和/或计划的后台任务。

查看为简单的重复任务实现 IBackgroundTask ( sample ),为更复杂的定时重复任务或计划的一次性任务实现 IScheduledTaskHandler ( sample )。

于 2013-06-27T11:42:19.340 回答