Reputation: 6452
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
Upvotes: 1
Views: 2553
Reputation: 901
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();
}
}
Upvotes: 2
Reputation: 10758
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.
Upvotes: 0