如何用ninject注入石英的工作? [英] how to inject quartz's job with ninject?
问题描述
我在应用程序中使用ninject和quartz.net,我想用ninject注入作业,但是我不知道该怎么做,因为我所知道的是jobdetail是由Jobimpl类而不是实例创建的,例如:
I use ninject and quartz.net in my application and I want to inject job with ninject,But I do not know how to ,because all I know is that jobdetail is created by class of Jobimpl instead of an instance,such as:
JobBuilder.Create<SomeJob>()
有人知道吗?
推荐答案
您将必须实现Quartz.Spi.IJobFactory
-使用IResolutionRoot
创建作业(有关实现,请参见下文).
然后配置调度程序以使用它:Quartz.IScheduler.JobFactory = kernel.Get<NinjectJobFactory>();
(或者:Quartz.IScheduler.JobFactory = new NinjectJobFactory(kernel);
)
You'll have to implement an Quartz.Spi.IJobFactory
- which uses an IResolutionRoot
to create the job (see below for implementation).
Then configure the scheduler to use it: Quartz.IScheduler.JobFactory = kernel.Get<NinjectJobFactory>();
(or, alternatively: Quartz.IScheduler.JobFactory = new NinjectJobFactory(kernel);
)
public class NinjectJobFactory : IJobFactory
{
private readonly IResolutionRoot resolutionRoot;
public NinjectJobFactory(IResolutionRoot resolutionRoot)
{
this.resolutionRoot = resolutionRoot;
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
return (IJob)this.resolutionRoot.Get(bundle.JobDetail.JobType);
}
public void ReturnJob(IJob job)
{
this.resolutionRoot.Release(job);
}
}
这篇关于如何用ninject注入石英的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!