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

Access object from Ninject when setting up Topshelf and Quartz.Net console application

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

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

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

任何help/suggestions将不胜感激。

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

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