Quartz.net in WCF

I have a service:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class PremieraInteraction : ServiceInit, IPremieraInteraction
{
    public PremieraInteraction()
    {
        ISchedulerFactory schedFact = new StdSchedulerFactory();
        // get a scheduler
        IScheduler sched = schedFact.GetScheduler();
        sched.Start();

        // construct job info
        IJobDetail jobDetail = JobBuilder.Create<PremieraUpdate>().WithIdentity("PremieraUpdateJob").Build();

        ITrigger trigger =
        TriggerBuilder.Create().WithIdentity("PremieraUpdateTrigger").StartNow().WithSimpleSchedule(
        x => x.WithIntervalInSeconds(10)).Build();

        sched.ScheduleJob(jobDetail, trigger); 
    }

}

This is the job:

public class PremieraUpdate:IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Debug.WriteLine("Fire");
        }
    }

The problem is that it works only once. Why does the scheduler not repeat every 10 seconds?

Answers


I would suggest you to instantiate the factory and scheduler in the Application_Start event of Global.asax.cs and store them in static public properties.

  public class Global : System.Web.HttpApplication
  {
    public static ISchedulerFactory Factory;
    public static IScheduler Scheduler;

    protected void Application_Start(object sender, EventArgs e)
    {
      Factory = new StdSchedulerFactory();
      Scheduler = Factory.GetScheduler();
    }
  }

Now inside the service you can access the scheduler through the Global property.

Ex.

Global.Scheduler

I hope you are using the WCF service to schedule new jobs to the Quartz server that will be typically running in a windows service. Instead of scheduling the job in the constructor you can do that in a method that will take the details of the job and finally schedule and run it.

If you go with this implementation you don't need to mark the service as singleton.


It is quite probable that the scheduler is garbage collected once the service has finished the operation. This is as expected since you don't have anything that refers to the scheduler besides the service.

Normally WCF services as created per call. This means that a new instance of the service is created for each call to the service. In most cases this is fine, for it helps avoiding problems with concurrency, but it may be confusing too.

In your case you need a persistent reference to the scheduler. There are many ways to do that, anything from a static variable on the service to some sort of scheduler repository that knows of all schedulers and their lifetimes. This depends on your usage.

Just to test, you could add a static field to your service class. Instansiate this in the service call and the scheduler should call the operation every 10 seconds as required.

But this also depends on where you are hosting the service (web servce, service, console app?)


Need Your Help

Centralized error handling in VB6

vb6 error-handling

I have the following method that all the error handlers call:

Which Java web framework best accommodates web designers?

java web-frameworks tapestry

Which Java web framework best supports the "web UI designer" role? That is, which framework lets you: