简体   繁体   中英

C# Quartz.net scheduler job as windows service not executing though window service is running

I am new to Topshelf and Quartz.net.

I want to run a scheduler job using c# quartz.net and as a windows service. I have created a windows service and did all necessary setup to invoke the scheduler job. I am not getting any error and the window service is started successfully. But when i place debug point in the scheduler job, it is not being executed even the time i set up has reached.

Program.cs

internal class Program
{
    private static readonly IUnityContainer Container = UnityConfig.GetConfiguredContainer();
    static void Main(string[] args)
    {
        HostFactory.Run(serviceConfig =>
        {
            serviceConfig.Service<ISchedulerService>(serviceInstance =>
            {
                serviceInstance.ConstructUsing(name => Container.Resolve<ISchedulerService>());
                serviceInstance.WhenStarted(execute => execute.Start());
                serviceInstance.WhenStopped(execute => execute.Stop());
            });
            serviceConfig.RunAsLocalSystem();
            serviceConfig.SetDescription("Scheduler");
            serviceConfig.SetDisplayName("Scheduler");
            serviceConfig.SetServiceName("Scheduler");

            serviceConfig.StartAutomatically();
        });
    }
} 

ScheduleService.cs

public class SchedulerService : ISchedulerService
{
    private readonly IScheduler _scheduler;
    private readonly DateTimeOffset _startTime = new DateTimeOffset(new DateTime(2016, 05, 01, 08, 30, 00, DateTimeKind.Utc).ToLocalTime());
    public SchedulerService(IScheduler scheduler)
    {
        _scheduler = scheduler;
    }
    public void Start()
    {
        StartScheduledJobs();
    }

    public void Stop()
    {
        _scheduler.Shutdown(true);
    }

    private void StartScheduledJobs()
    {
        try
        {
            SchedulePrsReportExportJob();
            _scheduler.Start();
        }
        catch (Exception ex)
        {

        }
    }

    private void SchedulePrsReportExportJob()
    {
        var jobDetail = JobBuilder.Create<MyJob>()
            .WithIdentity("job1", "group1")
            .Build();

        var trigger = TriggerBuilder.Create()
            .WithIdentity("trigger1", "group1")
            //.WithSchedule(CronScheduleBuilder.CronSchedule("0 1 0 ? * *"))  //minute past midnight everyday
            //.StartAt(_startTime)
            //.WithSchedule(CronScheduleBuilder.CronSchedule("0 0/5 * ? * *"))  //minute past midnight everyday
            .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(16, 18))
            .StartNow()
            .Build();

        _scheduler.ScheduleJob(jobDetail, trigger);
    }
}

Job

public class MyJob : IJob
{
    private readonly ISomeService _service;

    public ExportPrsLiveReportJob(ILogProvider logProvider, ISomeService service)
    {
        _service = service;
    }

    public async void Execute(IJobExecutionContext context)
    {
        var data = await _service.Get();
    }
}

Can anyone help me what am i doing wrong here?

Thanks

I had a similar problem to this. I had a test project that worked and my actual project that would not work.

The problem seems to be related to the length of the service name and/or the use of dots in the name.

I've not found anything in the Quatrz source code to substantiate this though. From my local testing the only difference between a working and non-working service was the service name.

I had a similar problem and found that NOT having a default constructor was the problem.

So, something like this:

public class ExportPrsLiveReportJob : IJob
{
    private readonly ISomeService _service;

    public ExportPrsLiveReportJob(ILogProvider logProvider, ISomeService service)
    {
        _service = service;
    }

    public ExportPrsLiveReportJob() : this(<however your system does dependency resolution for ILogProvider and ISomeService>){

    }

    public async void Execute(IJobExecutionContext context)
    {
        var data = await _service.Get();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM