1. 创建项目(中文版选用辅助角色服务,英文版直接选用worker Service即可)
2. 添加依赖包
Microsoft.Extensions.Hosting //worker service 必要包 Microsoft.Extensions.Hosting.Systemd //部署到Linux系统并使用systemctl管理时使用 Microsoft.Extensions.Hosting.WindowsServices //部署到Windows,使用sc管理时使用
3. 修改Program.cs文件
#添加 .UseSystemd() .UseWindowsService()
4. Windows启动说明
- 发布(使用命名和vs都可以实现)
- 使用sc.exe管理
#创建服务名 sc.exe create ServiceName binPath=C:\ServiceName.exe #启动服务 sc.exe start ServiceName #查看服务状态 sc.exe query ServiceName #停止服务 sc.exe stop ServiceName #删除服务 sc.exe delete ServiceName
5. Linux启动说明
- 发布(同windows)
- 上传至Linux
- 编写systemctl管理文件
vim /etc/systemd/system/ServiceName.service #配置内容 ********************************************** [Unit] Description=Servie Information [Service] Type=notify WorkingDirectory=/fileAddrss ExecStart=/fileAddress/ServiceName SyslogIdentifier=Servicelog User=system user name Environment=.dotnet环境变量 TimeoutStopSec=300 [Install] WantedBy=multi-user.target
- systemctl管理
#开启服务 systemctl start ServiceName #查看服务状态 systemctl status ServiceName #停止服务 systemctl stop ServiceName #服务重启 systemctl restart ServiceName