In Program .cs
public static class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
});
}
And Create Worker.cs file in which you need to write these lines of code for starting your Scheduler Services
public class Worker : BackgroundService
{
private readonly string l_ClassName = "Worker";
public Worker()
{
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
string l_FuncName = "OnStart";
using (new FuncTracer(l_ClassName, l_FuncName))
{
try
{
Logger.LogInformation(l_ClassName, l_FuncName, "Starting the Service");
var ScheduleServices = new ScheduleService().GetScheduleServices();
foreach (ScheduleJobServicesDto item in ScheduleServices)
{
if (item.ServiceEnabled)
switch (item.JobServiceId)
{
case 1:
DataAvailabilityAndReconciliationService l_DataAvailabilityAndReconciliationService = new DataAvailabilityAndReconciliationService();
l_DataAvailabilityAndReconciliationService.DoWork(123);
break;
}
}
await Task.Delay(1000, stoppingToken);
}
catch (Exception ex)
{
Logger.LogError(l_ClassName, l_FuncName, ex.Message);
}
}
}
public override Task StopAsync(CancellationToken cancellationToken)
{
string l_FuncName = "OnStart";
Logger.LogInformation(l_ClassName, l_FuncName, "Stopping the Service");
base.StopAsync(cancellationToken);
Logger.LogInformation(l_ClassName, l_FuncName, "The Service Is Stopped");
return Task.Delay(0, cancellationToken);
}
}