I am now using Ninject 2.2.1.4, with my MVC3, i'm success to config Ninject run with it, but i don't know how to make Ninject run with Quartz.Net in my MVC3 Can anyone help?
Asked
Active
Viewed 4,367 times
1 Answers
20
Create a JobFactory that uses Ninject
public class NinjectJobFactory : IJobFactory
{
private readonly Func<Type, IJob> jobFactory;
public NinjectJobFactory (Func<Type, IJob> jobFactory)
{
this.jobFactory = jobFactory;
}
public IJob NewJob(TriggerFiredBundle bundle)
{
return this.jobFactory(bundle.JobDetail.JobType);
}
}
and a QuarzSchedulerProvider
public class QuartzSchedulerProvider : Provider<IScheduler>
{
private readonly IJobFactory jobFactory;
private readonly IEnumerable<ISchedulerListener> listeners;
private readonly ISchedulerFactory schedulerFactory;
public QuartzSchedulerProvider(
ISchedulerFactory schedulerFactory,
IJobFactory jobFactory,
IEnumerable<ISchedulerListener> listeners)
{
this.jobFactory = jobFactory;
this.listeners = listeners;
this.schedulerFactory = schedulerFactory;
}
protected override IScheduler CreateInstance(IContext context)
{
var scheduler = this.schedulerFactory.GetScheduler();
scheduler.JobFactory = this.jobFactory;
foreach (var listener in this.listeners)
{
scheduler.AddSchedulerListener(listener);
}
return scheduler;
}
}
and a SchedulerFactoryProvider
public class QuartzSchedulerFactoryProvider : Provider<ISchedulerFactory>
{
protected override ISchedulerFactory CreateInstance(IContext context)
{
var properties = new NameValueCollection();
properties["quartz.dataSource.DataSource.connectionString"] = "Your connection string";
properties["quartz.dataSource.DataSource.provider"] = "Your provider";
properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz ";
properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
properties["quartz.jobStore.dataSource"] = "DataSource";
properties["quartz.jobStore.useProperties"] = "true";
return new StdSchedulerFactory(properties);
}
}
and configure
Bind<IJobFactory>().To<NinjectJobFactory>();
Bind<ISchedulerFactory>().ToProvider<QuartzSchedulerFactoryProvider>();
Bind<IScheduler>().ToProvider<QuartzSchedulerProvider>().InSingletonScope();
Bind<Func<Type, IJob>>().ToMethod(ctx => t => (IJob)ctx.Kernel.Get(t));
If you need some ISchedulerListener e.g. for logging bind them here too.
Inject an instance of IScheduler where you want to add Jobs and most likely you have to do property injection of an instance into global.asax too. But note I havn't used Quarz in MVC context yet as I think Scheduled Tasks do not belong into a Web App but rather into a service running on the same server.

Remo Gloor
- 32,665
- 4
- 68
- 98
-
Thanks so much Remo :),let me try it – Hieu Nguyen Trung Jul 20 '11 at 04:08
-
@Remo: should this.ResolutionRoot near the top be this.kernel or did you intend to rename the var? (or is ResolutionRoot something in IJobFactory?) – Ruben Bartelink Jul 20 '11 at 15:51
-
Yes you are right. I'didn't rename everything when I copied it from my solution. I normally use IResolutionRoot instead of IKernel. – Remo Gloor Jul 20 '11 at 16:58
-
@Remo: Makes sense, I like the idea. (HAD SAID: Have taken the liberty of editing the answer). Didnt, can you do it (dont know if IResolutionRoot is resolvable or whether you ctor inject IKernel and then downcast and stash that). (And I personally would demand a Func
and use that in the factory). Having said all that, you could just leave it as it illustrates the principle Just Fine Right Now. – Ruben Bartelink Jul 21 '11 at 08:13 -
@Ruben Bartelink: You are absolute right. Passing a Func is even better. I'll update the answer – Remo Gloor Jul 21 '11 at 09:09
-
If you were execute a simple job using this DI implementation in global.asax, what would it look like? – asunrey Dec 05 '13 at 01:01
-
I believe `scheduler.AddSchedulerListener(listener);` should be `scheduler.ListenerManager.AddSchedulerListener(listener);` – atconway Sep 26 '14 at 23:25