title: Quartz.Net Dependency Injection .Net Core
body: <p>In my project I have to use Quartz but I don't know what i do wrong.</p>
  
  <p>JobFactory:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class IoCJobFactory : IJobFactory
  {
      private readonly IServiceProvider _factory;
  
      public IoCJobFactory(IServiceProvider factory)
      {
          _factory = factory;
      }
      public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
      {
          return _factory.GetService(bundle.JobDetail.JobType) as IJob;
      }
  
      public void ReturnJob(IJob job)
      {
          var disposable = job as IDisposable;
          if (disposable != null)
          {
              disposable.Dispose();
          }
      }
  }
  </code></pre>
  
  <p>QuartzExtensions:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static class QuartzExtensions
  {
      public static void UseQuartz(this IApplicationBuilder app)
      {
          app.ApplicationServices.GetService&lt;IScheduler&gt;();
      }
  
      public static async void AddQuartz(this IServiceCollection services)
      {
          var props = new NameValueCollection
          {
              {"quartz.serializer.type", "json"}
          };
          var factory = new StdSchedulerFactory(props);
          var scheduler = await factory.GetScheduler();
  
          var jobFactory = new IoCJobFactory(services.BuildServiceProvider());
          scheduler.JobFactory = jobFactory;
          await scheduler.Start();
          services.AddSingleton(scheduler);
      }
  }
  </code></pre>
  
  <p>And when I try run my Job (class have dependency injection) i always get Exception becouse:
  </p>
  
  <pre><code>_factory.GetService(bundle.JobDetail.JobType) as IJob;
  </code></pre>
  
  <p>is always null.</p>
  
  <p>My class implement <code>IJob</code> and in startup.cs I add:</p>
  
  <pre class="lang-cs prettyprint-override"><code>services.AddScoped&lt;IJob, HelloJob&gt;();
  services.AddQuartz();
  </code></pre>
  
  <p>and 
  </p>
  
  <pre><code>app.UseQuartz();
  </code></pre>
  
  <p>I using standard .net Core dependency injection:
  </p>
  
  <pre><code>using Microsoft.Extensions.DependencyInjection;
  </code></pre>