Quartz.Net Dependency Injection .Net Core

quartz net core example
quartz.net dashboard
quartz.net scheduler example c#
.net core scheduler
topshelf quartz .net core
quartz net mvc
quartz net download
quartz.net logging

In my project I have to use Quartz but I don't know what i do wrong.

JobFactory:

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();
        }
    }
}

QuartzExtensions:

public static class QuartzExtensions
{
    public static void UseQuartz(this IApplicationBuilder app)
    {
        app.ApplicationServices.GetService<IScheduler>();
    }

    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);
    }
}

And when I try run my Job (class have dependency injection) i always get Exception becouse:

_factory.GetService(bundle.JobDetail.JobType) as IJob;

is always null.

My class implement IJob and in startup.cs I add:

services.AddScoped<IJob, HelloJob>();
services.AddQuartz();

and

app.UseQuartz();

I using standard .net Core dependency injection:

using Microsoft.Extensions.DependencyInjection;

This is how I did it in my application. Instead of adding the Scheduler to the ioc I only add the factory

services.AddTransient<IJobFactory, AspJobFactory>(
                (provider) =>
                {
                    return new AspJobFactory( provider );
                } );

My job factory pretty much looks the same. Transient does not really matter as I only use this once anyway. My use Quartz extension method then is

public static void UseQuartz(this IApplicationBuilder app, Action<Quartz> configuration)
        {
            // Job Factory through IOC container
            var jobFactory = (IJobFactory)app.ApplicationServices.GetService( typeof( IJobFactory ) );
            // Set job factory
            Quartz.Instance.UseJobFactory( jobFactory );

            // Run configuration
            configuration.Invoke( Quartz.Instance );
            // Run Quartz
            Quartz.Start();
        }

The Quartz class is Singleton as well.

Dependency Injection for Quartz.NET in .NET Core, NET core techniques. Introduction. Quartz.NET is a handy library that allows you to schedule recurring tasks via implementing IJob interface. The article shows how to perform dependency injection when using Quartz.NET library employing standard .NET Core DI container library. Also, we&#39;ll focus on a couple of other useful .NET core techniques.

This is just a simple sample of my solution to solve IoC problem:

JobFactory.cs

public class JobFactory : IJobFactory
    {
        protected readonly IServiceProvider Container;

        public JobFactory(IServiceProvider container)
        {
            Container = container;
        }

        public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        {
            return Container.GetService(bundle.JobDetail.JobType) as IJob;
        }

        public void ReturnJob(IJob job)
        {
            (job as IDisposable)?.Dispose();
        }
    }

Startup.cs

public void Configure(IApplicationBuilder app, 
            IHostingEnvironment env, 
            ILoggerFactory loggerFactory,
            IApplicationLifetime lifetime,
            IServiceProvider container)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc();

            // the following 3 lines hook QuartzStartup into web host lifecycle
            var quartz = new QuartzStartup(container);
            lifetime.ApplicationStarted.Register(quartz.Start);
            lifetime.ApplicationStopping.Register(quartz.Stop);
        }

QuartzStartup.cs

public class QuartzStartup
    {
        private IScheduler _scheduler; // after Start, and until shutdown completes, references the scheduler object
        private readonly IServiceProvider container;

        public QuartzStartup(IServiceProvider container)
        {
            this.container = container;
        }

        // starts the scheduler, defines the jobs and the triggers
        public void Start()
        {
            if (_scheduler != null)
            {
                throw new InvalidOperationException("Already started.");
            }

            var schedulerFactory = new StdSchedulerFactory();
            _scheduler = schedulerFactory.GetScheduler().Result;
            _scheduler.JobFactory = new JobFactory(container);
            _scheduler.Start().Wait();

            var voteJob = JobBuilder.Create<VoteJob>()
                .Build();

            var voteJobTrigger = TriggerBuilder.Create()
                .StartNow()
                .WithSimpleSchedule(s => s
                    .WithIntervalInSeconds(60)
                    .RepeatForever())
                .Build();

            _scheduler.ScheduleJob(voteJob, voteJobTrigger).Wait();
        }

        // initiates shutdown of the scheduler, and waits until jobs exit gracefully (within allotted timeout)
        public void Stop()
        {
            if (_scheduler == null)
            {
                return;
            }

            // give running jobs 30 sec (for example) to stop gracefully
            if (_scheduler.Shutdown(waitForJobsToComplete: true).Wait(30000))
            {
                _scheduler = null;
            }
            else
            {
                // jobs didn't exit in timely fashion - log a warning...
            }
        }
    }  

consider that you should register your service into the container (in my case VoteJob) in advance. I implement this based on this answer. I hope it can be helpful.

Quartz.Net Dependency Injection .Net Core, This is how I did it in my application. Instead of adding the Scheduler to the ioc I only add the factory services.AddTransient<IJobFactory, AspJobFactory>(  The article shows how to perform dependency injection when using Quartz.NET library employing standard .NET Core DI container library. Also, we'll focus on a couple of other useful .NET core techniques.

I got the same issue.

I update from

services.AddScoped<IJob, HelloJob>();

to

services.AddScoped<HelloJob>();

then it works.

_factory.GetService(bundle.JobDetail.JobType) as IJob; will not be null :)

Creating a Quartz.NET hosted service with ASP.NET Core, NET Core Dependency Injection ~ 7 min read. Quartz.NET is a .NET Standard 2.0 NuGet package, so it should be easy to install in your  The article shows how to perform dependency injection when using Quartz.NET library employing standard .NET Core DI container library. Also, we'll focus on a couple of other useful .NET core techniques. Tagged with quartznet, di, netcore, csharp.

Using scoped services inside a Quartz.NET hosted service with ASP , NET Core 3.1! NET Core Dependency Injection ~ 5 min read. works, using Scoped dependency injection services in your Quartz jobs is  Quartz-with-ASP.Net-Core. This project is an example of what Quartz.Net framework is and how can it be used in ASP.NET Core Web application. Getting Started. Before you proceed further, please make sure you've basic understanding of how ASP.NET Core MVC web application works. Also you need to have some basic idea about Quartz.Net framework.

Dependency Injection for Quartz.NET, Quartz.NET is a full-featured, open source job scheduling system that can be used from the smallest apps to large scale enterprise systems (stole this from t Dependency Injection for Quartz.NET. Aug 15, 2016 • • • Quartz .NET NET is configuring dependency injection (DI) for their jobs. WriteLine(speed >= 5 ? I'm trying to run up quartz.net service in asp.net mvc 4 application where we are using dependency injection & services. In this application I need quartz for sending emails with daily period. But whats the strange I can't use DI in quartz.net code because it's broke if I'm add constructor to it.

Quartz.DependencyInjection.NetCore 1.0.0, DependencyInjection.NetCore 1.0.0. Quartz.Net integration for .NET Core IoC container. Package Manager .NET CLI; PackageReference; Paket  Browse other questions tagged c#.net dependency-injection quartz.net simple-injector or ask your own question. The Overflow Blog Podcast 235: An emotional week, and the way forward

Comments
  • Did you installed your Jobs in your DI?
  • Ye i have only one job so i installed it by services.AddScoped<IJob, HelloJob>();
  • Quartz.Instance is in my excample scheduler ? And Action<Quartz> what is it? becouse Quartz in my app "Quartz is a namespace but is used like a type"
  • Quartz is a custom class I made. Here is the code gist.github.com/Mats391/50cbf7f9c0291e623565dd0cc1baace3
  • You are very helpful. Can You tell me in your startup.cs you don't use services.AddQuartz(); ? And UseQuartz need to have configuration so how you add this in your Startup.cs?
  • Here is the Quartz related stuff from my Startup.cs link
  • @donex93 I do not understand. On the Quartz you can add and remove jobs as you please. For my purposes I never need to remove jobs, so I did not bother with that.