Quartz.net作为服务无法配置quartz_job.xml

时间:2015-06-08 14:46:35

标签: c# windows-services quartz.net

使用时

            var properties = new NameValueCollection();
            properties["quartz.plugin.triggHistory.type"] = "Quartz.Plugin.History.LoggingJobHistoryPlugin";

            properties["quartz.plugin.jobInitializer.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin";
            properties["quartz.plugin.jobInitializer.fileNames"] = "quartz_jobs.xml";
            properties["quartz.plugin.jobInitializer.failOnFileNotFound"] = "true";
            properties["quartz.plugin.jobInitializer.scanInterval"] = "120";

            // First we must get a reference to a scheduler
            _schedulerFactory = new StdSchedulerFactory(properties);
            _scheduler = _schedulerFactory.GetScheduler();

windows service / quartz无法解析quartz_jobs.xml的路径。 如果我作为控制台运行它可以正常工作。

  public static void StartJobs()
    {
        try
        {
            _logger = LogManager.GetCurrentClassLogger();

            var properties = new NameValueCollection();
            properties["quartz.plugin.triggHistory.type"] = "Quartz.Plugin.History.LoggingJobHistoryPlugin";

            properties["quartz.plugin.jobInitializer.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin";
            properties["quartz.plugin.jobInitializer.fileNames"] = "quartz_jobs.xml";
            properties["quartz.plugin.jobInitializer.failOnFileNotFound"] = "true";
            properties["quartz.plugin.jobInitializer.scanInterval"] = "120";

            // First we must get a reference to a scheduler
            _schedulerFactory = new StdSchedulerFactory(properties);
            _scheduler = _schedulerFactory.GetScheduler();

            // start the schedule 
            _scheduler.Start();
        }
        catch (Exception ex)
        {
            _logger.Error(ex);
            throw new Exception(ex.Message);
        }
    }

2 个答案:

答案 0 :(得分:0)

如果它仍然不起作用,请将该文件作为嵌入资源包含在项目中,将操作设置为“始终复制”,以确保。然后提供quartz属性的完整文件路径:

Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "quartz_jobs.xml")

答案 1 :(得分:0)

我知道该线程来自2015年,但是我找不到有关在Windows服务中使用quartz.net的任何信息。就我而言,我将.Net Core 2.1通用主机作为与Windows_Services.xml一起使用的Windows服务,在我的appsettings.json文件中进行了引用。当Windows服务启动时,将查找quartz_job.xml,并尝试在c:\ windows \ system32中找到它。但是我的quartz_job.xml位于我的可执行文件所在的位置。我在其仓库的Quaztz \ Util \ FileUtil.cs中找到了方法ResolveFile,据说在其中放置了一个“〜”作为相对文件。所以我将appsettings.json更改为

 "plugin": {
      "jobInitializer": {
        "type": "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz.Plugins",
        "fileNames": "~\\quartz_jobs.xml"
      }

现在Windows服务可以读取quartz_jobs.xml。我希望如果您更改

 properties["quartz.plugin.jobInitializer.fileNames"] = "quartz_jobs.xml";

properties["quartz.plugin.jobInitializer.fileNames"] = "~\\quartz_jobs.xml";

它也应该起作用。