在设置Topshelf和Quartz.Net控制台应用程序时从Ninject访问对象

时间:2016-04-16 21:21:25

标签: c# scheduled-tasks ninject quartz.net topshelf

在配置Ninject / Topshelf / Quartz.Net设置期间使用对象的正确方法是什么?

我创建了一个IocModule来将我的界面绑定到一个具体的类:

public class IocModule : NinjectModule
{
    public override void Load()
    {
        Bind<IConfiguration>().To<JsonConfiguration>().InSingletonScope();
    }
}

在Quartz.Net设置运行时我需要一些来自此配置的值,但使用IConfiguration configuration = new JsonConfiguration();似乎不是正确的方法:

namespace Question {
    public class Program {
        public static int Main(string[] args) {
            var exitCode = HostFactory.Run(c => {
                c.UseNinject(new IocModule());

                // How can I get this from Ninject?
                IConfiguration configuration = new JsonConfiguration();
                configuration.Load();
                c.Service<Service>(sc => {
                    sc.ConstructUsingNinject();
                    sc.WhenStarted((service, control) => service.Start(control));
                    sc.WhenStopped((service, control) => service.Stop(control));
                    sc.UseQuartzNinject();
                    sc.ScheduleQuartzJob(q => q.WithJob(() =>
                        JobBuilder.Create<IvansLauncher>().Build())
                            .AddTrigger(() =>
                                TriggerBuilder.Create()
                                    .StartNow()
                                    .WithSchedule(CronScheduleBuilder
                                        .MonthlyOnDayAndHourAndMinute(
                                            configuration.DayToRun,
                                            configuration.HourToRun,
                                            configuration.MinuteToRun))
                                    .Build()));
                });
                c.EnablePauseAndContinue();
                c.EnableShutdown();
                c.StartAutomaticallyDelayed();
                c.RunAsLocalSystem();
            });
            return (int) exitCode;
        }
    }
}

任何帮助/建议将不胜感激。

1 个答案:

答案 0 :(得分:4)

我相信您可以使用NinjectBuilderConfigurator访问内核:

var config = NinjectBuilderConfigurator.Kernel.Get<IConfiguration>();