ASP.Net MVC 3、Ninject 和 Quartz.Net - 如何?

发布于 2024-11-25 02:31:19 字数 105 浏览 2 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒岛晴空 2024-12-02 02:31:19

创建一个使用 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

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);
    }
}

的 JobFactory并配置

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文