在Global.asax Application_Start中调度asp.net(4.x)和quartz.net(3.0.7.0)quartz_jobs.xml

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

对于我在lime.net 3.x之前在iis 8下运行的小型家庭使用项目以及它所采用的异步方向,一切都很好。由于我有动力跟随3.x的一般演变,我遇到运行时问题,我无法在线找到解决方案。

有人可以告诉我一个如何在asp.net 4.x global.asax application_start方法中使用quartz_jobs.xml文件安排作业的示例。

我得到的运行时错误如下:System.InvalidOperationException:'可能无法在已完成的任务上调用Start。'

运行的代码:

Task<IScheduler> scheduler;
protected void Application_Start(object sender, EventArgs e)
{
  StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
  scheduler = stdSchedulerFactory.GetScheduler();
  scheduler.Start(); /* the line that is responsible for the runtime error */
}

我知道在IIS下调度作业的反对意见,但在考虑后选择这样做,所以任何关于如何避免这种错误的例子将不胜感激。

亲切的问候。

c# asp.net quartz.net global-asax
1个回答
0
投票

石英3中的GetScheduler()不会返回IScheduler。它返回Task<IScheduler>。这两种类型,TaskIScheduler都有Start()方法,这是巧合。

要使用Task<IScheduler>这样的同步方法将IScheduler转换为Application_Start,首先必须等待任务结束然后获得结果。

var realScheduler = scheduler.GetAwaiter().GetResult();
realScheduler.Start().Wait(); // you have to wait here too, because IScheduler.Start is also async
© www.soinside.com 2019 - 2024. All rights reserved.