0

We have migrated from .NET Framework to .NET 5 to deploy our application to both linux and windows.

Originally we were using Windows Application, and to support cross platform deployment we’ve migrated to Console Application.

Is there an option to run it as a background process?

This suggests to convert to a Windows Application but that’s not an option.

Thanks.

Philip L
  • 339
  • 1
  • 3
  • 14
  • https://www.ben-morris.com/running-a-net-core-console-application-as-a-windows-service/ – abdusco Jun 19 '21 at 19:40
  • What does this program do? How it is called? – Andre.Santarosa Jun 19 '21 at 19:42
  • For a program that needs to run periodically, Task Scheduler is a good option. For a program that needs to run continuously, a Windows Service is a good option. In some cases, you might even be able to just put the program in a user's "Startup" folder. Note that these are both platform-specific details. Windows, Linux, Mac OS, etc. all have different ways to deal with this. In all cases, the details mainly affect what's _outside_ of your program, i.e. how it's invoked, rather than the code in the program itself. – Peter Duniho Jun 19 '21 at 20:05

1 Answers1

0

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);
        }
    }
Asad
  • 617
  • 2
  • 8
  • 23