logo Have you ever needed or thought of scheduling  some work of your enterprise .NET application?  Maybe you have heard about Quartz.NET. I’ve heard about it recently and I had to use it in my system. I was delighted with the possibilities it offered. But very quick I noticed that I cannot inject dependency into my jobs by using my IoC container. Fortunately, I’ve found the solution and I would like to share it  in this post.

Quartz.NET at a glance

Quartz.NET is an open source job scheduling system. It is a port of a very popular open source Java job scheduling framework, Quartz . Quartz.NET is pure .NET library written in C#. Quartz.NET developers say that this project is feature-wise equal to Quartz Java 1.6 excluding Java specifics.

Quartz.NET has a very good home page, where you can find the list of features, API documentation and a very useful tutorial.

Create Quartz.NET’s job using IoC and DI

Below, I describe how to use Quartz.NET with IoC and DI container, in my case- Castle Windsor. If you don’t know much about these patterns, you can find out more about  it in this great article. I’ve used these patterns to simplify dependency creation in my job objects. Of course, I have other benefits, which resulted from using IoC and DI, but in this post the main focus is on how to instantiate job using IoC an DI. In Quartz.NET tutorial I’ve found one useful hint:”The job is instantiated via the JobFactory configured on the Scheduler. 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.”

Let’s do it in 5 steps

1. Create your own implementation of JobFactory. How? I’ve used implementation of Quartz.NET’s SimpleJobFactory.

IServiceLocator is the most important in this class. This is interface for IoC container location. When you use it, you can get instance of registered job type. 2. Now make implementation of IServiceLocator.

3. In this step, create some simple job with dependencies. Mine looks like in this way:

4. The next step is registering components in Castle container:

  • implementation of IServiceLocator
  • IoCJobFactory
  • IScheduler using ISchedulerFactory GetScheduler method – this provides setting IoCJobFactory on the Scheduler component
  • Job component

You can do it either in code or in configuration file. I’ve done it  in code.

5. The last step involves resolving Scheduler component.

That’s all.

Final thoughts

In my application these solutions work fine. I don’t worry about creating new objects that jobs are dependent on. If you decide to use Quartz.NET, I hope you also use this solution to build professional applications using IoC and DI patterns. Share your opinion and experience with us below or meet us on Twitter: @GOYELLO.

Thanks to Karol Świder for helping me write this blog post.

IT guy with head full of ideas, strongly oriented on achieving great goals in life. Currently working for GOYELLO, place where I grow.

  • You might be interested in this Quartz facility for Windsor: http://bugsquash.blogspot.com/2009/03/windsor-f
    It supports listener integration and doesn't depend on CommonServiceLocator

  • maciejgren

    Thank you! for sure I will have a look there!

  • Pingback: opnbefuc()

  • Striz

    I think it's too complex for small things that needs to be scheduled. I know for a fact that lots of guys have this need but are too lazy 🙂 I actually prefer to use components or services if I need anything complex for small things. So I saw this post of yours and thought I'd mention some online solutions that I tried before. The WebBasedCron at webbasedcron.com is simple to use but doesn't offer free stuff. The Web Scheduler at scheduler.codeeffects.com has really cool UI. It gives you a free schedule but has too much of extras, I think. The Web Service Scheduler at wsscheduler.com is oversimplified but is fine if you don't need any complex schedules.

  • ilker

    Thanks for your article. But I did not understand one think.
    How can I tell Quartz use my JobFactory. In this case IoCJobFactory.
    Registrations like as you did.

    I got following error:
    11.05.2010 14:28:24 [DEBUG] Quartz.Simpl.SimpleJobFactory – Producing instance of Job 'myTaskGroupId.myTaskUniqueId', class=MySchedulerService.Quartz.QuartzJob
    11.05.2010 14:28:24 [ERROR] Quartz.Core.ErrorLogger – An error occured instantiating job to be executed. job= 'myTaskGroupId.myTaskUniqueId'
    Quartz.SchedulerException: Problem instantiating class 'MySchedulerService.Quartz.QuartzJob' —> System.ArgumentException: Cannot instantiate type which has no empty constructor
    Parameter name: QuartzJob
    at Quartz.Util.ObjectUtils.InstantiateType(Type t)
    at Quartz.Simpl.SimpleJobFactory.NewJob(TriggerFiredBundle bundle)
    — End of inner exception stack trace —
    at Quartz.Simpl.SimpleJobFactory.NewJob(TriggerFiredBundle bundle)
    at Quartz.Core.JobRunShell.Initialize(QuartzScheduler sched, TriggerFiredBundle firedBundle) [See nested exception: System.ArgumentException: Cannot instantiate type which has no empty constructor
    Parameter name: QuartzJob
    at Quartz.Util.ObjectUtils.InstantiateType(Type t)
    at Quartz.Simpl.SimpleJobFactory.NewJob(TriggerFiredBundle bundle)]
    11.05.2010 14:28:24 [INFO] Quartz.Simpl.RAMJobStore – All triggers of Job myTaskGroupId.myTaskUniqueId set to ERROR state.

    It still uses SimpleJobFactory.


  • ilker

    I got it.
    I should set Scheduler.JobFactory = this.JobFactory;

    And it works.

  • It looks good,I have learn a recruit!
    Recently,I found an excellent online store, the “http://www.air-jordan-18.com are completely various, good quality and cheap price,it’s worth buying!

  • Hhe article's content rich variety which make us move for our mood after reading this article. surprise, here you will find what you want! Recently, I found some wedsites which commodity is research-laboratory colorful of fashion. Such as that worth you to see. Believe me these websites won’t let you down.

  • Awesome! Great post, it just saved me an hour of trial and error!! Should be included in the documention somewhere. I had no problems porting this to StructureMap

  • Pingback: Skype and Agile software development were really hot on our blog this year()

  • Pingback: How to use Quartz.net with ASP.NET | Ask Programming & Technology()

  • Pingback: How do I create a Quartz.NET’s job requiring injection with autofac | Technology & Programming()

  • Chris Knight

    Thank you very much for this post. It was very helpful. I was able to get this working with Ninject dependency injection.

  • Pingback: How to use Quartz.net with ASP.NET - Technology()