.NET CORE 3.0 Worker Services added a new project templates, you can write long-running background service, and can easily be deployed into service windows or linux daemons.
step:
1. Create a Project:
After successfully created:
Only two categories, compared with the ASP.NET Core Web Application, Program like nothing's changed, startup class disappeared, and adds to the DI container service worker in.
worker class inherits BackgroundService
Run can know, once every 1 sec Print
Windows Service Deployment
Package references: Microsoft.Extensions.Hosting.WindowsServices -v3.0.0
then added program.cs .UseWindowsService ()
Then we come to publish a project:
first step:
method one:
Released their desired position
Method Two:
Open a command, first find the project: cd + project path
Then: dotnet publish -c Release -o plus address released - "
dotnet publish -c Release -o C:\WorkerPub\Release
(Mainly, if not find the file or something, because you do not first find the project path)
Step 2: Create windows service (using sc.exe tool)
sc .exe create binPath = path name of the executable program - "
sc .exe create TimingWorkService binPath=C:\WorkerPub\WorkerService1.exe
After successful, we open the service to view:
Open a command: Enter services.msc, to see really successful
Check the service status (.exe can not)
sc query TimingWorkService
STATE state is STOPPED explain stopped
Then we start the service
sc start TimingWorkService
STATE状态是START说明启动了,也可以查看一下
测试后,执行停止,删除
sc.exe stop TimingWorkService sc.exe delete TimingWorkService
执行成功后,服务里面也找不到了。找得到就说明没有成功
Linux守护程序运行 :
1.添加包:
Microsoft.Extensions.Hosting.Systemd NuGet
2. UseSystemd()加入到Program.cs