Here is the link to the "home page" for this blog series.

We are going to learn here how to:

Install Visual Studio and .NET Core SDK

To get started install Visual Studio from here. Community edition is just good enough and it is free. Just remember to install ASP.NET and web development workload. To check that start Visual Studio Installer

Then click on Modify

Visual Studio Installer Modify

and check that

ASP.NET and web development workload is installed or install it.

Now download a latest .NET Core SDK from here

.NET Core SDK

Remember to download sdk because you are going to build apps.

To check what you have installed, run following command

dotnet --list-sdks

My output looks like this

dotnet --list-sdks

Create a Worker Service using Visual Studio

Open the Visual Studio and click on Create a new project

Visual Studio

Start to write worker service

Worker Service

Choose Worker Service Template and click on Next

Provide Project name, Location and Solution name, then click on Create.

Create a Worker Service Using .NET Core CLI

If you have created projects a couple of times, it's better and faster to do it using command line and .NET Core CLI provides you all functionality you need.

Navigate to the folder where you want to have your code and create a new folder by using mkdir command. I called it CreateAndInstallWorkerService

 cd \blog\azureDevOps\WorkerService\
 mkdir CreateAndInstallWorkerService

Navigate to the folder and create a new solution with the same name as folder using dotnet command

cd CreateAndInstallWorkerService\
dotnet new sln

Create a new folder, I called it DemoWorkerService and navigate there

mkdir DemoWorkerService
cd DemoWorkerService

Create Worker Service template

dotnet new worker

Now add the project to the solution

cd ..
dotnet sln CreateAndInstallWorkerService.sln add DemoWorkerService\DemoWorkerService.csproj

Folder structure

Always check in the file explorer or command line what you have created.

The structure should be like this

Solution

And

Worker Service

Run Project

Now it's time to open Visual Studio and run the project.

Double click on CreateAndInstallWorkerService.sln in the file explorer

Visual Studio

Press F5 or Debug -> Start Debugging

Start Debugging

And observe the output window showing you something like this

Add code to the Source Control

Now we have a running component, it is time to add it to the Source Control.

Initiate a git repository by running

git init

and check that you get .git folder by running

ls -a

Don't forget to create .gitignore file.

Navigate to gitignore.io

gitignore

Create gitignore file for Visual Studio and save it as .gitignore in your folder.

Now commit your changes with message.

git add .
git commit -m "Initial"

It's time to push your git repository to the server. I am fun of Azure DevOps and going to use it. You can choose where you want to host you code. It's a plenty of choices like GitHub, Bitbucket and so on.

How to create Azure DevOps account, you can read here, and project here. To check out my public Azure DevOps project I created for this blog post check here.

To push you code to the Azure DevOps Project, create first repo in your Azure DevOps project by clicking on Repos -> New Repository

I called mine CreateAndInstallWorkerService

Click on Create.

You get some help how to push your local repository to the Azure DevOps Repo

I am going to use Push an existing repository from command line

Open your terminal Copy and Paste first command from Azure DevOps and run it. Mine looks like this

git remote add origin
git remote add origin https://[email protected]/sergeydotnet/WorkerService/_git/CreateAndInstallWorkerService

And then Copy and Paste the second command

git push -u origin --all

Check that your Azure DevOps repo gets some code, should looks like this

Azure DevOps with code

Install Worker Service as Services

Now it's time to install our worker service as Services (former Windows Services). This is the easiest way to run Worker Services on Windows.

We are going to use a command line utility which is preinstalled on Windows called sc. Here is documentation. To install our newly created worker service we should do a couple of things.

  • First install Microsoft.Extensions.Hosting.WindowsServices Nuget package. In the Visual Studio, right click on the project
Manage NuGet Packages

Click on Manage NuGet Packages...

Search for Microsoft.Extensions.Hosting.WindowsServices

NuGet

Choose the package and click on Install.

Check your csproj file by clicking on it in the Visual Studio. Should look like this

<Project Sdk="Microsoft.NET.Sdk.Worker">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.4" />
    <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.4" />
  </ItemGroup>
</Project>
This is a good habit always to check your csproj file.

Now open a program.cs file

and add UseWindowsService()  to the CreateHostBuilder function

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace DemoWorkerService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .UseWindowsService()
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>();
            });
    }
}

Here is the  program.cs class. Build your application by clicking on Build->Build Solution

Or just run on the same level with your sln file

dotnet build
  • Second install the worker service as Service
sc create DemoWorkerService binPath=C:\blog\azureDevOps\WorkerService\CreateAndInstallWorkerService\DemoWorkerService\bin\Debug\netcoreapp3.1\DemoWorkerService.exe

Remember to run your command line as Administrator.

Created Service Sucess

Remember to paste your path to the .exe file. Now check your Services. To open services click on Windows key, then start writing Services

Check that your service is installed.

Services

You can use sc utility also to Stop, Start and Delete services.

sc start DemoWorkerService
sc stop DemoWorkerService
sc delete DemoWorkerService

Event Viewer

Now Start your Service and check Event Viewer

To open Event Viewer click on Windows key and start writing Event Viewer

Event Viewer

Check that you are not getting any errors.

Event Viewer

Now we have created our component and checked that everything working as expected and even committed and pushed our changes to the Source Control. Now it is about time to set up our CI/CD pipeline using Azure DevOps in the next blog post here.

Here is the link to the "home page" for this blog series.

Thanks for nice post image to Nattu Adnan for sharing their work on Unsplash.

Let me know in the comments if you experienced any troubles or if you have any feedbacks. Don't forget to subscribe to get news from Sergey .NET directly to your mailbox.


Home page for this blog series

Download Visual Studio

Download latest .NET Core SDK

gitignore.io

Create Azure DevOps Account

Create Azure DevOps project

Azure DevOps public project Worker Service

sc.exe documentation