Quartz Job Scheduler in Windows Service

耗尽温柔 提交于 2019-12-04 20:55:43
jvilalta

The problem with your code is that the reference to the scheduler falls out of scope after OnStart finishes running. SomeScheduler should be defined somewhere outside of the function so that it doesn't get garbage collected. As an example, this is how the Quartz.Net server project does it (using Topshelf but I think you get the idea. This is the main program, that installs the service. Notice it returns a reference to the server so the host can keep a reference to it.

public static class Program
{
    /// <summary>
    /// Main.
    /// </summary>
    public static void Main()
    {
        HostFactory.Run(x =>
                            {
                                x.RunAsLocalSystem();

                                x.SetDescription(Configuration.ServiceDescription);
                                x.SetDisplayName(Configuration.ServiceDisplayName);
                                x.SetServiceName(Configuration.ServiceName);

                                x.Service(factory =>
                                              {
                                                  QuartzServer server = new QuartzServer();
                                                  server.Initialize();
                                                  return server;
                                              });
                            });
    }
}

In the QuartzServer class the scheduler is an instance variable:

public class QuartzServer : ServiceControl, IQuartzServer
{
    private readonly ILog logger;
    private ISchedulerFactory schedulerFactory;
    private IScheduler scheduler; // code snipped....

}

As @granadaCoder points out, it might be easier to simply re-use the server that is provided.

Here are links to QuartzServer and Program.cs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!