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

WindowsServiceTempate repo

To take a look at my repository, use following command

https://sergeydotnet@dev.azure.com/sergeydotnet/WindowsService/_git/WindowsServiceTemplate

Start Visual Studio 2019, if you dont have one download from here.

Visual Studio start display

Choose Continue without code.

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

New project

Select Blank Solution and click Next

Blank Solution

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

Configure your new project

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

Structure on the disk

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...

New Project...

Choose Console App (.NET Framework)

Add a new project

Click on Next

Don't select Windows Service template I will explain later why.

Make sure you choose .NET Framework and click Next.

Configure your new project

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 oss.

Solution Explorer

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

Added console applicaiton 

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...

Add Reference

Search for System.ServiceProcess

Reference Manager

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

Windows Service Start Failure

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.

Output

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 tast and write Services  and observe our new service

Services

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.


"Home page" for this blog serie.

Download Visual Studio

My Public Azure DevOps WindowsService project

WindowsServiceTemplate repository

Create Azure DevOps account

Create a new project Azure DevOps

Environment.UserInteractive Property

How to create a Windows service by using Sc.exe