3

I have a .NET windows service that is using TopShelf and Quartz.Net. My scheduled jobs only get triggered when I run my Topshelf console app (.NET framework 4.7.2) locally. When I deploy them as a Windows service on the same machine or on a remote machine, the triggers don't fire. What am I doing wrong?

Here is the code I have:

class Program
{
    static void Main(string[] args)
    {
        var services = new ServiceCollection();

        var serviceProvider = Bootstrapper.GetServiceProvider(services);


        HostFactory.Run(configurator =>
        {
            configurator.SetServiceName("MyWindowsService");
            configurator.SetDisplayName("MyWindowsService");
            configurator.SetDescription("MyWindowsService");

            configurator.RunAsNetworkService();

            configurator.Service<MyWindowsService>(serviceConfigurator =>
            {
                var jobFactory = serviceProvider.GetRequiredService<IJobFactory>();

                serviceConfigurator.ConstructUsing(() => new MyWindowsService(jobFactory));

                serviceConfigurator.WhenStarted((service, hostControl) =>
                {
                    service.OnStart();
                    return true;
                });
                serviceConfigurator.WhenStopped((service, hostControl) =>
                {
                    service.OnStop();
                    return true;
                });
            });
        });
    }
}

And here is the Bootstrapper Code:

class Bootstrapper
{
    public static IServiceProvider GetServiceProvider(IServiceCollection services)
    {
        services.AddSingleton<IJobFactory>(provider =>
        {
            var jobFactory = new JobFactory(provider);
            return jobFactory;
        });

        services.AddTransient<DownloadJob>();            

        RegisterConfigurations(services);

        var serviceProvider = services.BuildServiceProvider();
        return serviceProvider;
    }

    public static void RegisterConfigurations(IServiceCollection services)
    {
        if (services == null)
        {
            return;
        }
    }
}

Here is the Windows Service:

public class MyWindowsService
{
    private readonly IJobFactory jobFactory;

    public MyWindowsService(IJobFactory jobFactory)
    {
        this.jobFactory = jobFactory;
    }

    public async void OnStart()
    {
        var schedulerFactory = new StdSchedulerFactory();
        var scheduler = await schedulerFactory.GetScheduler();
        scheduler.JobFactory = jobFactory;
        await scheduler.Start();
        ScheduleAllJobs(scheduler);
    }

    private async void ScheduleAllJobs(IScheduler scheduler)
    {
        var downloadJobDetails = GetDownloadJobDetails();
        var downloadTrigger = GetDownloadTrigger();
        await scheduler.ScheduleJob(downloadJobDetails, downloadTrigger);
    }

    private IJobDetail GetDownloadJobDetails()
    {
        return JobBuilder.Create<DownloadJob>()
            .WithIdentity(JobKey.Create("Downloader", "Download Job"))
            .Build();
    }

    private ITrigger GetDownloadTrigger()
    {
        return TriggerBuilder.Create()
            .WithIdentity(new TriggerKey("Downloader", "Download Job"))
            .StartNow()
            .WithSimpleSchedule(builder =>
            {
                builder.WithIntervalInSeconds(5)
                    .RepeatForever();
            })
            .Build();
    }

    public void OnStop()
    {

    }
}

Here is the relevant code from the job:

public class DownloadJob : IJob
{
    public Task Execute(IJobExecutionContext context)
    {
        // Do something
        return Task.CompletedTask;
    }
}
walen
  • 7,103
  • 2
  • 37
  • 58
Yasir
  • 1,595
  • 5
  • 23
  • 42

0 Answers0