Hosting on Ubuntu Linux 20.04

Case Details

May 31, 2022

Install .Net 6.0 Worker Service on Ubuntu Linux 20.04

A Step by Step guide to creating Worker Service and Install on Linux

1. Create a new project and select the Worker Service template

Install-.Net-6.0-Worker-Service

2. Specify your project name, project location, and solution name to configure your Worker Service

Install-.Net-6.0-Worker-Service

3. Specify framework to .Net 6.0 latest one

Install-.Net-6.0-Worker-Service

4. Now your project is almost ready, locate Program.cs. It should look something like this.

Install-.Net-6.0-Worker-Service

The Host is responsible for application startup and lifetime management. CreateDefaultBuilder creates this runtime host to run the application. CreateDefaultBuilder does a few things: configuring Kestrel server, setting the root directory, load app configuration (appsettings.json, appsettings. {Environment}.json), and configuring logging.

5. To run Worker Service as Windows Service.

You have to install Microsoft.Extensions.Hosting.WindowsServices NuGet package. Add the UseWindowsService() call to the host builder.

Install-.Net-6.0-Worker-Service

6. To run Worker Service as Linux Daemon.

You have to install Microsoft.Extensions.Hosting.Systemd NuGet package.

Install-.Net-6.0-Worker-Service

7. Look at Worker.cs

This is the background worker which does the actual job.

Install-.Net-6.0-Worker-Service

8. Publish service on the local folder

Install-.Net-6.0-Worker-Service

9. To copy Service to Linux Server

Create a folder under your user directory. Here devadmin is my username. (We can't directly copy Service to Publish folder due to permission denied. So, we have to copy it into this folder and then we will copy it to publish folder)

sudo mkdir -p /home/devadmin/workerservice

mkdir -p command

With the help of mkdir -p command, you can create subdirectories of a directory. It will create a parent directory first if it doesn’t exist. But if it already exists, then it will not print an error message and will move further to create sub-directories. https://www.javatpoint.com/linux-mkdir-p

In this step, we are going to copy that folder to Linux Virtual Machine for doing that we are going to use WinSCP. URL to Download WinSCP: – https://winscp.net/eng/index.php

Install-.Net-6.0-Worker-Service

10. Creating a publish folder in Linux Server

sudo mkdir -p /opt/workerservice

11. Copy files to the newly created folder /opt/workerservice.

sudo cp -r /home/devadmin/workerservice/ /opt/

cp -r command

Option ‘r’ with the copy command can be used to copy a directory including all its content from a source directory to the destination directory. https://www.javatpoint.com/linux-cp-r

12. We need to create a new service file, so use the below command to create a service file.

sudo nano /etc/systemd/system/workerservice.service

and edit its content with the following content to it

[Unit]
Description=Mind Coder Worker Service

[Service]
Type=notify
WorkingDirectory=/opt/workerservice

ExecStart=/usr/bin/dotnet /opt/workerservice/WorkerService.dll

Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target

Then press Ctrl + s to save its content and press Ctrl + x to jump back to the normal window.

13. Reload daemon configs to pick up the new file.

This will pick up the new file and allow you to start and stop the service.

sudo systemctl daemon-reload

14. To manage the Service

Start the services manually.

sudo systemctl start workerservice

View status of the service

sudo systemctl status workerservice

Stop the service

sudo systemctl stop workerservice

Enable service to it runs on boot

sudo systemctl enable workerservice

Disable service to it runs on boot

sudo systemctl disable workerservice

Examine the Logs

This will allow for the logs to be viewed for the service.

sudo journalctl -u workerservice

Note on logs

The Logs levels on the .NETCore side will match the Systemd logging levels as this

Log Level Syslog Level systemd name
Trace/Debug 7 debug
Information 6 info
Warning 4 warning
Error 3 err
Critical 1 critical

so look at only critical entries you can go

sudo journalctl -p 3 -u workerservice

Project Details
  • Technology: .Net 6.0
  • Database: MS SQL
  • Platform: Linux Ubuntu 20.04

Related Case Studies