Short answer: recreate your project using the "Windows service" template as described here:
https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer
then install it using installutil or using the integrated installer
Longer answer:
A service is a "Console application" with specific entry points, so this is the very basic code to create the service:
using System.ServiceProcess;
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
this.ServiceName = "Service1";
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
}
as you can see, the main class inherits and implements the "ServiceBase" class and override a few methods. The main methods are "OnStart" (called when you start a service) and "OnStop", called when you stop it.
There are a lot of other properties and methods, described here (or pressing F12 in visual studio on the class name):
https://learn.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicebase
Taking a look at the "main", you can see how it works:
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
a few things you have to remember:
a service is executed in c:\windows\system32 as base path. Do not use relative paths.
OnStart has to be quick. Do not perform long operations in that method. Best course of action is perform all start checks and Launch a thread.
change the main in this way to allow debugging (obviously TestMode should be the code to test):
bool isInteractive = Environment.UserInteractive || args.Contains("--interactive");
if (isInteractive)
((Service1)ServicesToRun[0]).TestMode();
else
ServiceBase.Run(ServicesToRun);
Once built you .exe file, use installutil to install it as a service
Install a Windows service using a Windows command prompt?