.NET CORE 3.0 adds new project templates for Worker Services, which can be used to write long-running background services and easily deploy them as Windows services or Linux daemons. If the installed vs2019 is the Chinese version, Worker Services becomes a worker role.
Linux system environment: CentOS 8 x64-bit
Create a new Worker Service project
First, open VS 2019 to create a new Worker Service project.
Linux services, need to add:
If it's a Windows service, you'll need to add:
If you want to run a set of programs in multiple places, you can just install two packages at the same time. Next in CreateHostBuilder, add UseWindowsService() and UseSystemd().
The function implemented by the project is to write logs to the /test/file.txt file, and the main code is as follows:
The WriteFile task code is as follows:
Due to permission issues, the program cannot automatically create the /test folder, which will be discussed below.
Deploy services to CentOS
YesInstall the .NET Core runtime(Excluding ASP.NET Core support)
If you want to install a runtime that supports ASP.NET Core, you can run the following command:
[root@localhost~]# dotnet --list-runtimes Microsoft.NETCore.App 3.1.7 [/usr/lib64/dotnet/shared/Microsoft.NETCore.App]
Create a /test folder and give permissions as follows:
Under the /etc/systemd/system folder, create the itsvse.service service configuration file as follows:
Add service accounts and create folders
Publish and upload Job.Service to the server /home/dotnetuser/job directory, and publish the configuration as follows:
Turn on the service
After the service starts, you can see that you can write files normally, as shown in the following figure:
Source code download:
Tourists, if you want to see the hidden content of this post, please Reply
(End)
|