I have a windows forms application that does some timely call to webservices. I did it as a windows forms because it was simpler to track comunications and tests. But now i need it to run as a windows service so it can be installed in a windows server an run unattended. The samples i have seen all start with a worker service project. But i want to make my windows forms app run as a service.
With .NET 4.5 i managed to build a service start project like this:
static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new CRDService() 
            };
            ServiceBase.Run(ServicesToRun);
        }
    }
And a service loader like this:
public partial class CRDService : ServiceBase
    {
        private int processId;
        private string filePath;
        public CRDService()
        {
            InitializeComponent();
        }
        public void Verify(string[] args)
        {
            this.OnStart(args);
            // do something here...  
            this.OnStop();
        }  
        protected override void OnStart(string[] args)
        {
            var location = new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath;
            var path = Path.GetDirectoryName(location);
            string appName = ConfigurationManager.AppSettings["AppName"];
            var serverPath = Path.Combine(path, appName);
            Process cmd = new Process();
            cmd.StartInfo.FileName = serverPath;
            cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            using (var f = File.Create(Path.Combine(path, "TestFile.txt")))
            {
                filePath = f.Name;
                string msg = "Process started at " + DateTime.Now.ToString();
                f.Write(Encoding.ASCII.GetBytes(msg),0,msg.Length);
            }
            cmd.StartInfo.Arguments = filePath;
            cmd.Start();
            processId = cmd.Id;  
        }
        protected override void OnStop()
        {
            Process process = null;
            try
            {
                process = Process.GetProcessById((int)processId);
            }
            finally
            {
                if (process != null)
                {
                    process.Kill();
                    process.WaitForExit();
                    process.Dispose();
                }
                File.Delete(filePath);
            }  
        }
    }
But this doesn't work with .NET 6 and BackgroundService.
Can someone tell me if there is a way to do this in .NET 6/7?
 
                        
Managed to resolve the problem. Here is the same solution for .NET 6/7.