This article is a mirror article of machine translation, please click here to jump to the original article.

View: 39816|Reply: 3

[.NET Core] A service based on .NET Core on Linux

[Copy link]
Posted on 2020-10-29 14:20:02 | | | |
.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, pleaseReply


(End)




Previous:ASP.NET WebApi uses MessageHandlers to unify the return interface model format
Next:HttpSelfHostServer based console web API service
 Landlord| Posted on 2020-10-30 09:28:44 |
// Sample output:
//     info: ExampleHostedService[0]
//           1. StartAsync has been called.
//     info: ExampleHostedService[0]
//           2. OnStarted has been called.
//     info: Microsoft.Hosting.Lifetime[0]
//           Application started. Press Ctrl+C to shut down.
//     info: Microsoft.Hosting.Lifetime[0]
//           Hosting environment: Production
//     info: Microsoft.Hosting.Lifetime[0]
//           Content root path: .. \app-lifetime\bin\Debug\net5.0
//     info: ExampleHostedService[0]
//           3. OnStopping has been called.
//     info: Microsoft.Hosting.Lifetime[0]
//           Application is shutting down...
//     info: ExampleHostedService[0]
//           4. StopAsync has been called.
//     info: ExampleHostedService[0]
//           5. OnStopped has been called.


 Landlord| Posted on 2020-11-6 13:53:00 |
public abstract class BackgroundService : IHostedService, IDisposable

The IHostedService interface provides a convenient way to start background tasks in ASP.NET Core web applications (in .NET Core 2.0 and later) or on any process/host (starting with .NET Core 2.1 using IHost). Its main advantage is that it can be done when the host itself is about to shut downThere is a chanceMake a normal cancellation to clean up the code for background tasks.
 Landlord| Posted on 2021-4-27 15:57:28 |
Register the backend service extension method

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com