Here is the link to the "home page" for this blog serie.
I created a new Azure DevOps project and called WindowsService. How to create Azure DevOps account read this blog and how to create new project read this. Create new repository and call it for example WindowsServiceTempate

To take a look at my repository, use following command
git clone
https://[email protected]/sergeydotnet/WindowsService/_git/WindowsServiceTemplate
Start Visual Studio 2019, if you don't have one download from here.

Choose Continue without code.
Create new solution by clicking on File ->New -> Project... or just use shortcut Ctrl+Shift+N

Select Blank Solution and click Next

Choose the name for the project and choose location. Choose Place solution and project in the same directory and Click Create.

Always check the File Explorer what Visual Studio created for you.

Solution (*.sln) file should be on the same level with .git and .gitignore.
Now add new project, right click on Solution and Add->New Project...

Choose Console App (.NET Framework)

Click on Next
Don't select Windows Service template I will explain later why.
Make sure you choose .NET Framework and click Next.

Provide name, choose location where you pulled the project and choose lates .NET Framework. At the time I am writing it is 4.7.2.
Click on Create.
Visual Studio creates following structure for us.

Always check the File Explorer what really did you get. Suppose to be something like this

Build (Ctrl+ Shift+B) and Run (F5) application. Check that you don't have any errors. Commit and push your changes and learn to do it as fast as you achieve any kind of milestone. Like here our first milestone is: create new solution and add new console application. Done, Commit and Push.
Now we turn our console application into windows service. Add reference to the System.ServiceProcess.dll by click on References->Add Reference...

Search for System.ServiceProcess

Copy and paste this code
using System.ServiceProcess;
namespace WindowsServiceTemplate
{
class Program
{
static void Main(string[] args)
{
ServiceBase.Run(new MyService());
}
}
class MyService : ServiceBase
{
}
}
By calling ServiceBase.Run... we delegate control over to the Service Control Manager. And now we can't run this application by clicking F5 or Debug->Start Debugging.
We are getting following error message

But before we install our service as this error message pointing out, we want to have the possibility to run and debug our application using command line or Visual Studio. Add this code
using System;
using System.ServiceProcess;
namespace WindowsServiceTemplate
{
class Program
{
static void Main(string[] args)
{
if (Environment.UserInteractive)
{
using (var service = new MyService())
{
service.Start();
Console.WriteLine("Running, press any key to stop");
Console.ReadKey();
}
}
else
{
ServiceBase.Run(new MyService());
}
}
}
public class MyService : ServiceBase
{
public void Start()
{
//Do Something
}
}
}
Here we are utilizing Environment.UserInteractive. We are creating kind of two entry points to our application. Build and Run(F5) and check that our error message is gone.

Commit and push your changes.
Now when we have tested one entry point, it is time to test another one. We install our application as windows service. To do it we use the sc command
Open your favorite command line tool and run following command
sc create WindowsServiceTempate displayname="WindowsServiceTempate" binpath="C:\full\path\WindowsServiceTemplate\bin\Debug\WindowsServiceTemplate.exe"
If everything is correct, you get following message
[SC] CreateService SUCCESS
Now open windows services by clicking on windows key and write Services and observe our new service

Now click on start and observe that everything working nicely.
To delete the service use following command
sc delete WindowsServiceTempate
Here is the link to the "home page" for this blog serie.
Links in this blog
"Home page" for this blog serie.
My Public Azure DevOps WindowsService project
WindowsServiceTemplate repository
Create a new project Azure DevOps