Search code examples
c#scheduled-tasksninjectquartz.nettopshelf

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


What would be the correct way to use an object during the configuration of the Ninject/Topshelf/Quartz.Net setup?

I have created an IocModule to bind my interface to a concrete class:

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

I need a few values from this configuration during Quartz.Net setup of the time to run, but using IConfiguration configuration = new JsonConfiguration(); doesn't seem like it is the correct way to do this:

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

Any help/suggestions would be appreciated.


Solution

  • I believe you can access the Kernel using the NinjectBuilderConfigurator:

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