ASP.Net MVC 3,Ninject和Quartz.Net - 如何? [英] ASP.Net MVC 3, Ninject and Quartz.Net - How to?
问题描述
我现在使用Ninject 2.2.1.4,与我的MVC3,我是成功的配置Ninject运行它,但我不知道如何使Ninject与Quartz.Net在我MVC3运行
谁能帮助?
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?
推荐答案
创建使用Ninject的JobFactory一个
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);
}
}
和一个QuarzSchedulerProvider
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;
}
}
和一个SchedulerFactoryProvider
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);
}
}
和配置
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));
如果你需要一些ISchedulerListener例如这里记录绑定他们。
If you need some ISchedulerListener e.g. for logging bind them here too.
注入要在其中添加作业,最有可能你必须做一个实例的属性注入的Global.asax太IScheduler的一个实例。但是请注意我在MVC方面没有带使用的石英硅但因为我认为计划任务不属于成一个Web应用程序,而是在同一个服务器上运行的服务。
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.
这篇关于ASP.Net MVC 3,Ninject和Quartz.Net - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!