石英调度器。在 asp.net 应用程序启动期间安排作业

Quartz scheduler. Schedule job during asp.net application start

我想在应用程序启动期间使用任务调度程序创建线程。 感谢 this and this,我做到了,但是出了点问题,工作没有 运行ning,当然之前已经初始化了。

我的 class 开始前 运行:

[assembly: WebActivatorEx.PreApplicationStartMethod(
typeof(Application.App_Start.TaskScheduler), "Start")]
namespace Application.App_Start
{
    public static class TaskScheduler
{
    private static readonly IScheduler scheduler = new StdSchedulerFactory().GetScheduler();

    private static void CreateTaskToDeleteTmpFiles(Object sender)
    {
        scheduler.Start();

        //Create job which will be add to thread
        IJobDetail job = JobBuilder.Create<DeleteTmpJob>()
            .WithIdentity("ClearTmpFiles")
            .StoreDurably()
            .Build();

        //Create thread which run the job after specified conditions
        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("ClearTmpFiles")
            .StartAt(DateBuilder.FutureDate(1, IntervalUnit.Second))
            .Build();

        //Add Job and Trigger to scheduler
        scheduler.ScheduleJob(job, trigger);

    }


 }
}

我的工作class:

public class DeleteTmpJob : IJob
    {
        private IDocumentStore documentStore;
        private IUploaderCollection uploaderCollection;

        public DeleteTmpJob(IDocumentStore _documentStore, IUploaderCollection _uploaderCollection)
        {
            documentStore = _documentStore;
            uploaderCollection = _uploaderCollection;
        }

        public void Execute(IJobExecutionContext context)
        {
            documentStore.ClearTmpDirectory();
        }
    }

工作不 运行宁

有人可以帮忙吗?

我遇到了同样的问题,当我删除构造函数工作时。首先尝试调用基本构造函数,如果仍然无法正常工作,请尝试删除构造函数。

您是否尝试过在作业中使用空构造函数?

"Each (and every) time the scheduler executes the job, it creates a new instance of the class before calling its Execute(..) method. One of the ramifications of this behavior is the fact that jobs must have a no-arguement constructor."

您可能需要实现自己的 JobFactory 才能使用 DI。如何实现它取决于您使用的是哪个库。

"When a trigger fires, the JobDetail (instance definition) it is associated to is loaded, and the job class it refers to is instantiated via the JobFactory configured on the Scheduler.The default JobFactory simply calls the default constructor of the job class using Activator.CreateInstance, then attempts to call setter properties on the class that match the names of keys within the JobDataMap. You may want to create your own implementation of JobFactory to accomplish things such as having your application's IoC or DI container produce/initialize the job instance."

来源:see here