【问题标题】:ASP.Net MVC 3, Ninject and Quartz.Net - How to?ASP.Net MVC 3、Ninject 和 Quartz.Net - 如何?
【发布时间】:2011-07-21 17:17:58
【问题描述】:

我现在使用 Ninject 2.2.1.4 和我的 MVC3,我成功配置 Ninject 运行它,但我不知道如何让 Ninject 在我的 MVC3 中运行 Quartz.Net 有人可以帮忙吗?

【问题讨论】:

    标签: asp.net-mvc-3 quartz.net ninject-2


    【解决方案1】:

    创建一个使用 Ninject 的 JobFactory

    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

    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

    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,例如用于记录也将它们绑定在这里。

    在您想要添加作业的地方注入一个 IScheduler 实例,并且很可能您也必须将一个实例的属性注入到 global.asax 中。但请注意,我还没有在 MVC 上下文中使用过 Quarz,因为我认为计划任务不属于 Web 应用程序,而是属于在同一服务器上运行的服务。

    【讨论】:

    • 非常感谢 Remo :),让我试试
    • @Remo:靠近顶部的 this.ResolutionRoot 应该是 this.kernel 还是您打算重命名 var? (或者是 IJobFactory 中的 ResolutionRoot 吗?)
    • 是的,你是对的。从我的解决方案中复制它时,我没有重命名所有内容。我通常使用 IResolutionRoot 而不是 IKernel。
    • @Remo:有道理,我喜欢这个主意。 (HAD 说:冒昧地编辑了答案)。没有,你能做到吗(不知道 IResolutionRoot 是否可解析,或者你是否注入 IKernel 然后向下转换并隐藏它)。 (而且我个人会要求 Func 并在工厂中使用它)。说了这么多,你可以留下它,因为它说明了现在就很好的原则。
    • @Ruben Bartelink:你是绝对正确的。传递一个 Func 就更好了。我会更新答案
    猜你喜欢
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    相关资源
    最近更新 更多