asp.net-mvc-3 – ASP.Net MVC 3,Ninject和Quartz.Net – 如何?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – ASP.Net MVC 3,Ninject和Quartz.Net – 如何?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我现在正在使用Ninject 2.2.1.4,与我的MVC3,我成功配置Ninject运行它,但我不知道如何使Ninject运行在Quartz.Net在我的MVC3
谁能帮忙?

解决方法

创建一个使用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应用程序,而是属于在同一服务器上运行的服务.

原文链接:https://www.f2er.com/aspnet/249837.html

猜你在找的asp.Net相关文章