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;
services.AddScoped<IJob, HelloJob>();
– Controversial