I am using .NET MVC 4. All services are injected using Ninject. I am trying to schedule a job using Quartz. Right now, jobs are registered in Global.asax
as follows:
Global.asax
:
protected void Application_Start() {
// ... configuration stuff
ScheduledJobs.RegisterJobs();
}
ScheduleJobs.cs
has the ScheduledJobs
class, which creates jobs with triggers and adds them to a standard schedule.
In ScheduleJobs.cs
:
public class ScheduledJobs {
public static void RegisterJobs() {
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
IJobDetail job = JobBuilder.Create<JobUsingService>()
.WithIdentity("JobUsingService")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithDailyTimeIntervalSchedule(s =>
s.WithIntervalInHours(1)
.OnEveryDay()
.StartingDailyAt(new Quartz.TimeOfDay(DateTime.Now.Hour, DateTime.Now.Minute)))
.Build();
scheduler.ScheduleJob(job, trigger);
}
}
This is the job code:
public class JobUsingService : IJobUsingService, IJob {
private ISomeService someService;
public JobUsingService(ISomeService _someService) {
someService = _someService;
}
public void Execute(IJobExecutionContext context) {
someService.someStuff();
}
}
The problem is that JobUsingService
has to be initialized using Ninject so that SomeService
is injected into it (also by Ninject). Calling IJobDetail job = JobBuilder.Create<JobUsingService>().WithIdentity("JobUsingService").Build();
skips over the Ninject injection and creates a regular instance of the class without injecting the necessary services.
How can I create a job of type JobUsingService
using Ninject?
This SO answer suggests creating a NinjectJobFactory
, but I am not sure how to actually use this factory and create jobs.