1

I have a windows service which (at least used to) creates jobs from the xml config file. However I can't get it working.

This is my xml (jobs.config)

<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data
xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
<schedule>
  <job>
        <name>FirstJob</name>
        <group>DummyGroup</group>
        <description>This is FirstJob</description>
        <job-type>Test.TestJob, Test</job-type>
   </job>
  <trigger>
      <simple>
          <name>nativeJobExampleSimpleTrigger</name>
           <group>nativeJobExampleSimpleTriggerGroup</group>
            <description>Simple trigger to simply fire sample job</description>
            <job-name>FirstJob</job-name>
            <job-group>DummyGroup</job-group>
            <misfire-instruction>SmartPolicy</misfire-instruction>
            <repeat-count>5</repeat-count>
            <repeat-interval>5000</repeat-interval>
      </simple>
    </trigger>
</schedule>

and this is the code to create my job.

var jobs = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "jobs.config");
            
var properties = new NameValueCollection
                 {
                   ["quartz.scheduler.instanceName"] = "XmlConfiguredInstance",
                   ["quartz.threadPool.threadCount"] = "5",
                   ["quartz.threadPool.threadPriority"] = "Normal",
                   ["quartz.plugin.xml.type"] = "Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz",
                   ["quartz.plugin.xml.fileNames"] = jobs
                 };

var sf = new StdSchedulerFactory(properties);
sched = sf.GetScheduler();
sched.Start();

var jgn = sched.GetJobGroupNames();
var count = jgn.Count;

No matter what I try, the value of count is always zero and my job doesn't start. The path to the config file is correct (I tried renaming the file to 1jobs.config and got "File not found exception."

I have tried several examples of config and code from documentation, the result is the same. Moving properties to myservice.exe.config doesn't change anything either. Jobs.config has read access for everyone.

However, if I try the same code and the same config in a WinForms app, it works like it's supposed to.

Marko Juvančič
  • 5,792
  • 1
  • 25
  • 41

1 Answers1

0

In a windows service Assembly.GetExecutingAssembly().Location probably returns something like C:\Windows\system....

Either try

var jobs = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "jobs.config");

or even better try

               ["quartz.plugin.xml.fileNames"] = "~/jobs.config"

Quartz.NET 3.0 handles ~ correctly, perhaps 2.0 does too.

Rafał Rutkowski
  • 1,419
  • 10
  • 11
  • No, it returns the correct path. – Marko Juvančič Feb 17 '22 at 07:42
  • Sorry, I missed the fragment, where you mentioned about the path being correct. I cannot find any problems in your code. My setup is pretty much identical and it works in a windows service. Except I'm on Quartz v3. You have to enable log4net in your application to see exactly what Quartz is doing. – Rafał Rutkowski Feb 17 '22 at 10:53