dotNetCore创建Windows服务程序并安装服务

一、创建控制台程序

二、在项目中添加新建项,选择Windows服务类型。

此时会出现一个错误提示,这是因为尚未添加windows服务控制引用造成的。

三、添加Nuget包,System.ServiceProcess.ServiceController。

添加完成后错误提示就消失了。

四、更改main方法。

using System;
using System.ServiceProcess;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceBase[] services = new ServiceBase[] { new Service1() };
            ServiceBase.Run(services);
        }
    }
}

五、将程序发布为可执行文件。

点击编辑,将部署模式改为独立。

发布。

根据配置情况,在相应的目录内(例如:bin\Release\netcoreapp2.2\win-x86)即可看到可执行文件。

六、使用sc命令将可执行文件安装为服务。

安装服务:sc create testservice binpath="D:\Working\test\test\bin\Release\netcoreapp2.2\win-x86\test.exe"

查询服务:sc query testservice

启动服务:sc start testservice

停止服务:sc stop testservice

卸载服务:sc delete testservice

猜你喜欢

转载自www.cnblogs.com/tianjiuyi/p/11095964.html