Worker Service Generation and Deployment

There are more and more system platforms at present, and the demand for cross-platform programming is also increasing, so we need to develop a background service that can run on different platform operating systems, such as Windows, Linux, or macOS. Therefore, using .NET 8.0 is a good choice because it supports cross-platform and has good tool support.

In .NET 8.0, we can choose the Worker Service template because it is suitable for creating long-running background services. The next part is the cross-platform build. Add a runtime identifier (RID), such as win-x64, linux-x64, or osx-x64. Then use the dotnet publish command to publish executable files for different platforms.

1. Create a Worker Service project

We open Visual Studio, choose to create a project, then search for the Worker Service project and select and create it. After waiting for the creation to complete, we need to configure cross-platform support, as shown in the figure below. We need to modify the project file content to support various platforms.

Create a Worker Service project

2. Write service code

Currently, only basic code is written to display the service running process, as shown in the figure below:

Write basic code

In the Worker.cs file generated by default in the project, we write tasks that need to run for a long time in its overloaded task ExecuteAsync. As shown in the figure, the current time during the service running process is displayed every 1 second. Users can rewrite the code here according to their own task needs.

3. Configure platform support

Configure platform support

After the user completes the task function in Work.cs, the user needs to configure the corresponding platform support for the current service according to his platform properties. As shown in the figure above, the figure determines whether the current system is Windows or Linux, and different service processes will be started according to different systems.

4. Platform deployment

When the project is completed, the user can perform the project release task. The user needs to release different types of projects according to different platforms. For example, when the user releases a Windows service, the corresponding release of the Windows platform is required, as shown in the figure below:

Corresponding release for Windows platform

The current command will generate the corresponding version of Windows. When the release is successful, a Windows release file package will be displayed in the folder of the user’s corresponding project. Then the user needs to install the Windows service according to the package path, as shown below:

Install Windows Services

Run the current command in the Windows cmd command line, and the system will automatically install the service at the current corresponding file path location. When the service is successfully started, the task compiled by the user will be automatically run.

Linux service release and installation are similar, but the commands run are different. The following is a demonstration of Linux running commands.

Linux run commands

After successful release, create a service file /etc/systemd/system/myservice.service.

Create a service file

Then execute the command.

Then execute the command

Through the above steps and commands, you can get a cross-platform service running on Windows and Linux.

Update cookies preferences
WhatsApp

Get a Quick Quote!

x

    Get a Quick Quote!

    x