Azure Worker角色+ Ninject + Quartz

时间:2015-08-03 15:30:15

标签: azure ninject quartz-scheduler azure-worker-roles ninject-extensions

我基于现有的"核心"建立后台流程。我需要使用的代码(意思是.Core项目)。

核心代码基于Ninject。所以我使用Ninject.Extensions.Azure

此外,我构建的核心是某种与调度有关的后台处理,我需要Quartz。因此我安装了Ninject.Extensions.Quartz

现在我的问题是Quartz永远不会创建我的作业实例(无论无参数构造函数)。

查看创建的调度程序,看起来它不会返回Quart Extensions中的那个,但我找不到原因。

这是我的角色切入点:

public class WorkerRole : NinjectRoleEntryPoint
{
    private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
    private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false);

    private IScheduler scheduler;
    private IKernel kernel;
    public override void Run()
    {
        Trace.TraceInformation("Company.Services.Report is running");

        try
        {
            RunAsync(cancellationTokenSource.Token).Wait();
        }
        finally
        {
            runCompleteEvent.Set();
        }

    }

    protected override bool OnRoleStarted()
    {
        // Set the maximum number of concurrent connections
        ServicePointManager.DefaultConnectionLimit = 12;

        bool result = base.OnRoleStarted();

        Trace.TraceInformation("Company.Services.Report has been started");
        ConfigureScheduller();

        return result;
    }

    protected override void OnRoleStopped()
    {
        Trace.TraceInformation("Company.Services.Report is stopping");

        cancellationTokenSource.Cancel();
        runCompleteEvent.WaitOne();

        base.OnRoleStopped();

        Trace.TraceInformation("Company.Services.Report has stopped");
    }

    protected override IKernel CreateKernel()
    {
        kernel = new StandardKernel();
        kernel.Load(new CoreModule());
        return kernel;
    }

    private static async Task RunAsync(CancellationToken cancellationToken)
    {
        while (!cancellationToken.IsCancellationRequested)
        {
            Trace.TraceInformation("Working");
            await Task.Delay(1000, cancellationToken);
        }
    }
    private void ConfigureScheduller()
    {
        scheduler = kernel.Get<IScheduler>();

        // define the job and tie it to our WorkerJob class
        IJobDetail job = JobBuilder.Create<ReportProcessorJob>().Build();

        // Trigger the job to run now, and then repeat every 12 hours
        ITrigger trigger = TriggerBuilder.Create()
            .StartNow() //.WithCronSchedule("0 0 12 * * ?") //TODO: set good schedule start
            .WithSimpleSchedule(x => x
                .WithIntervalInSeconds(10)
                .RepeatForever())
            .Build();

        // Tell quartz to schedule the job using our trigger
        scheduler.ScheduleJob(job, trigger);

        scheduler.Start();
    }

}

我不包括该作业,因为它仅将调度信息提供给控制台以进行调试。

你觉得那里有什么不对吗?

1 个答案:

答案 0 :(得分:1)

我发现了问题!

问题的原因是Ninject找不到某些所需接口的实现。可悲的是,它没有说什么,只是没有工作......

如果它对某人有帮助,这就是我发现它的方式:

  1. 在Visual Studio中转到Debug - &gt;例外
  2. 检查&#34;投掷&#34;对于公共语言运行时异常
  3. 然后当你进行调试时,它会显示一个实际的异常,并带有一条非常明确的消息,说它无法解析IFoo。

    HTH