c#.NET MVC 4-如何创建使用Ninject服务的Quartz作业? [英] c# .NET MVC 4 - How do I create Quartz jobs that use Ninject services?

查看:88
本文介绍了c#.NET MVC 4-如何创建使用Ninject服务的Quartz作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.NET MVC4.所有服务都是使用Ninject注入的.我正在尝试使用Quartz安排工作.现在,作业在Global.asax中注册如下:

I am using .NET MVC 4. All services are injected using Ninject. I am trying to schedule a job using Quartz. Right now, jobs are registered in Global.asax as follows:

Global.asax:

protected void Application_Start() {
    // ... configuration stuff

    ScheduledJobs.RegisterJobs();
}

ScheduleJobs.cs具有ScheduledJobs类,该类创建带有触发器的作业并将其添加到标准计划中.

ScheduleJobs.cs has the ScheduledJobs class, which creates jobs with triggers and adds them to a standard schedule.

ScheduleJobs.cs中:

public class ScheduledJobs {

  public static void RegisterJobs() {
    IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();

    scheduler.Start();

    IJobDetail job = JobBuilder.Create<JobUsingService>()
        .WithIdentity("JobUsingService")
        .Build();

    ITrigger trigger = TriggerBuilder.Create()
        .WithDailyTimeIntervalSchedule(s =>
            s.WithIntervalInHours(1)
            .OnEveryDay()
            .StartingDailyAt(new Quartz.TimeOfDay(DateTime.Now.Hour, DateTime.Now.Minute)))
        .Build();

    scheduler.ScheduleJob(job, trigger);
  }
}

这是工作代码:

public class JobUsingService : IJobUsingService, IJob {

   private ISomeService someService;

   public JobUsingService(ISomeService _someService) {
       someService = _someService;
   }

   public void Execute(IJobExecutionContext context) {
       someService.someStuff();
   }
}

问题在于,必须使用Ninject初始化JobUsingService,以便将SomeService注入到其中(也是通过Ninject进行的).调用IJobDetail job = JobBuilder.Create<JobUsingService>().WithIdentity("JobUsingService").Build();会跳过Ninject注入,并创建该类的常规实例,而不会注入必要的服务.

The problem is that JobUsingService has to be initialized using Ninject so that SomeService is injected into it (also by Ninject). Calling IJobDetail job = JobBuilder.Create<JobUsingService>().WithIdentity("JobUsingService").Build(); skips over the Ninject injection and creates a regular instance of the class without injecting the necessary services.

如何使用Ninject创建类型为JobUsingService的作业?

How can I create a job of type JobUsingService using Ninject?

这样的答案建议创建一个NinjectJobFactory,但我不确定如何实际使用该工厂并创建工作.

This SO answer suggests creating a NinjectJobFactory, but I am not sure how to actually use this factory and create jobs.

推荐答案

最后,我们需要添加我们还必须进行以下更改:

We also had to make the following changes:

ScheduleJobs.cs中:

public class ScheduledJobs {

    public static void RegisterJobs() {
        // Create a new kernel and create the necessary bindings
        IKernel kernel = new StandardKernel();
        kernel.Bind<IJobUsingService>().To<JobUsingService>();

        // Create a scheduler and give it the Ninject job factory created earlier
        IScheduler scheduler = new StdSchedulerFactory().GetScheduler();
        scheduler.JobFactory = new NinjectJobFactory(kernel);

        // Create the job with the interface which will be injected
        IJobDetail job = JobBuilder.Create<IJobUsingService>()
             .WithIdentity("IJobUsingService")
             .Build();

         // Create the trigger
         ITrigger trigger = TriggerBuilder.Create()
             .WithDailyTimeIntervalSchedule(s =>
                 s.WithIntervalInHours(23)
                  .OnEveryDay()
                  .StartingDailyAt(new Quartz.TimeOfDay(DateTime.Now.Hour, DateTime.Now.Minute)))
             .Build();

         scheduler.ScheduleJob(job, trigger);
    }
}

这篇关于c#.NET MVC 4-如何创建使用Ninject服务的Quartz作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆