title: How do I create a Quartz.NET’s job requiring injection with autofac
body: <p>I am trying to get Quartz.net (2.1.2) to work with an IoC container (autofac), as I have services I need to use in the scheduled jobs. I have found similar posts on the subject, but I can't seem to find one with a specific registration example for autofac.</p>
  
  <p>The following post deals with the same issue I am having:</p>
  
  <p><a href="https://stackoverflow.com/questions/16653178/how-to-schedule-task-using-quartz-net-2-0">How to schedule task using Quartz.net 2.0?</a></p>
  
  <p>However, the part I believe I am missing is when the answer says "And don't forget to register the job in the IoC container". I am unsure how to do this exactly, as everything I have tried so far hasn't worked.</p>
  
  <p>In the following example, the "HelloJob" will run, but whenever I try to inject the releaseService into the "ReleaseJob" it refuses to run.</p>
  
  <p><strong>Update:</strong>
  I marked the code in the DependencyRegistration.cs section where I believe the issue is.</p>
  
  <p><strong>Update 2:</strong> 
  Some related links that are related to what I need to do and might help (I've already gone through them all but still cannot figure out how to get this working with autofac):</p>
  
  <p><em>HOW TO use Quartz.NET in PRO way?</em>
  - <a href="http://blog.goyello.com/2009/09/21/how-to-use-quartz-net-in-pro-way/" rel="noreferrer">http://blog.goyello.com/2009/09/21/how-to-use-quartz-net-in-pro-way/</a></p>
  
  <p><em>Autofac and Quartz.NET</em>
  - <a href="http://blog.humann.info/post/2013/01/30/Autofac-and-QuartzNET.aspx" rel="noreferrer">http://blog.humann.info/post/2013/01/30/Autofac-and-QuartzNET.aspx</a></p>
  
  <p><em>Constructor injection with Quartz.NET and Simple Injector</em>
  - <a href="https://stackoverflow.com/questions/14562176/constructor-injection-with-quartz-net-and-simple-injector/14562660#14562660">Constructor injection with Quartz.NET and Simple Injector</a></p>
  
  <p><em>ASP.Net MVC 3, Ninject and Quartz.Net - How to?</em>
  - <a href="https://stackoverflow.com/questions/6741599/asp-net-mvc-3-ninject-and-quartz-net-how-to">ASP.Net MVC 3, Ninject and Quartz.Net - How to?</a></p>
  
  <p>Here is the relevant code:</p>
  
  <p><strong>Global.asax</strong></p>
  
  <pre><code>    protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
  
          RegisterGlobalFilters(GlobalFilters.Filters);
          RegisterRoutes(RouteTable.Routes);
  
          var dependencyRegistration = new DependencyRegistration();
          dependencyRegistration.Register();
  
          ModelValidatorProviders.Providers.Clear();
          ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new ValidatorFactory()));
  
          DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
      }
  </code></pre>
  
  <p><strong>DependencyRegistration.cs</strong></p>
  
  <pre><code>public class DependencyRegistration
  {
      public void Register()
      {
          var builder = new ContainerBuilder();
  
          builder.RegisterControllers(Assembly.GetExecutingAssembly());
          builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly());
  
          // Validation
          builder.RegisterType&lt;ValidatorFactory&gt;()
              .As&lt;IValidatorFactory&gt;()
              .InstancePerHttpRequest();
  
          AssemblyScanner findValidatorsInAssembly = AssemblyScanner.FindValidatorsInAssembly(Assembly.GetExecutingAssembly());
          foreach (AssemblyScanner.AssemblyScanResult item in findValidatorsInAssembly)
          {
              builder.RegisterType(item.ValidatorType)
                  .As(item.InterfaceType)
                  .InstancePerHttpRequest();
          }
  
          // Schedule
          builder.Register(x =&gt; new StdSchedulerFactory().GetScheduler()).As&lt;IScheduler&gt;();
  
          // Schedule jobs
          builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(x =&gt; typeof(IJob).IsAssignableFrom(x));
  
          var container = builder.Build();
          DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  
          //Schedule
          IScheduler sched = container.Resolve&lt;IScheduler&gt;();
          sched.JobFactory = new AutofacJobFactory(container);
          sched.Start();
  
          IJobDetail job = JobBuilder.Create&lt;ReleaseJob&gt;()
                  .WithIdentity("1Job")
                  .Build();
  
          ITrigger trigger = TriggerBuilder.Create()
              .WithIdentity("1JobTrigger")
              .WithSimpleSchedule(x =&gt; x
                  .RepeatForever()
                  .WithIntervalInSeconds(5)
              )
              .StartNow()
              .Build();
  
          sched.ScheduleJob(job, trigger);
  
          job = JobBuilder.Create&lt;HelloJob&gt;()
                 .WithIdentity("2Job")
                 .Build();
  
          trigger = TriggerBuilder.Create()
              .WithIdentity("2JobTrigger")
              .WithSimpleSchedule(x =&gt; x
                  .RepeatForever()
                  .WithIntervalInSeconds(5)
              )
              .StartNow()
              .Build();
  
          sched.ScheduleJob(job, trigger);
      }
  }
  </code></pre>
  
  <p><strong>JobFactory.cs</strong></p>
  
  <pre><code>public class AutofacJobFactory : IJobFactory
  {
      private readonly IContainer _container;
  
      public AutofacJobFactory(IContainer container)
      {
          _container = container;
      }
  
      public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
      {
          return (IJob)_container.Resolve(bundle.JobDetail.JobType);
      }
  
      public void ReturnJob(IJob job)
      {
      }
  }
  </code></pre>
  
  <p><strong>ReleaseJob.cs</strong></p>
  
  <pre><code>public class ReleaseJob : IJob
  {
      private readonly IReleaseService _releaseService;
  
      public ReleaseJob(IReleaseService releaseService)
      {
          this._releaseService = releaseService;
      }
  
      public void Execute(IJobExecutionContext context)
      {
          Debug.WriteLine("Release running at " + DateTime.Now.ToString());
      }
  }
  
  public class HelloJob : IJob
  {
      public void Execute(IJobExecutionContext context)
      {
          Debug.WriteLine("Hello job at " + DateTime.Now.ToString());
      }
  }
  </code></pre>
  
  <p><strong>ReleaseServiceModel.cs</strong></p>
  
  <pre><code>public class ReleaseServiceModule : Module
  {
      protected override void Load(ContainerBuilder builder)
      {
          builder.RegisterType&lt;ReleaseService&gt;()
              .As&lt;IReleaseService&gt;()
              .InstancePerLifetimeScope();
      }
  }
  </code></pre>