首页 > 技术文章 > Windows服务的创建,安装,卸载

luna-hehe 2018-09-27 16:28 原文

我公司项目的产线系统要与WMS物流系统做借口对接,需要我创建一个windows服务的项目,里面含有7个服务

创建Windows服务:

1.如图:点击 windows->经典桌面->windows服务,填写项目名称点击确定即可,此时已有一个Service1.cs 

2.添加第二个服务,右击选择添加新建项-> 常规 -> windows服务 ,此时多了一个Service2.cs 

3.分别双击service1.cs和service2.cs 打开设计器 

4.在空白处右击 选择“添加安装程序”

5.添加完之后会多出ProjectInstaller.cs文件,并在此文件中会多出serviceInstaller1和serviceProcessInstaller1

6.双击service2.cs文件,右击选择“添加程序文件”,此时会在ProjectInstaller.cs文件中添加serviceInstaller2 

7.每一个服务的属性设置:

其中ServiceName的内容Service1是默认的,它要和windows服务项目的启动程序Program.cs中的名称一样

Program.cs中的个数不能少,不然会报:【.net服务 错误1083:配置成在该可执行程序中运行的这个服务不能执行该服务】

windows服务的安装:

首先需要生成将要安装的windows服务

注:windows服务调试特别麻烦,可以使用新建windowsform项目进行测试

从命令行运行 Installutil.exe 目录  命令,以项目中的已编译可执行文件所在的目录作为参数,安装服务:

1.因为Installutil.exe程序在 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ 目录下,需要通过cmd命令 "cd" 切换目录。

2.安装服务:installutil.exe E:\MyProject\WLZJ_Update\04_JieKou\WindowsService_1709_180704\bin\Debug\WindowsService_1709_180704.exe

 

找到exe文件所在目录:E:\MyProject\WLZJ_Update\04_JieKou\WindowsService_1709_180704\bin\Debug

再命令提示符中执行:installutil.exe E:\MyProject\WLZJ_Update\04_JieKou\WindowsService_1709_180704\bin\Debug\WindowsService_1709_180704.exe

windows服务的卸载:

1、以管理员身份运行命令行工具

2、进入InstallUtil.exe文件夹
cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 或 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

C:\Windows\system32>cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319\

3、卸载服务,如果路径带空格,用双引号包起来

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe /u "E:\MyProject\WLZJ_Update\04_JieKou\WindowsService_1709_180704\bin\Debug\WindowsService_1709_180704.exe"

4、卸载结果

正在开始卸载。
查看日志文件的内容以获得 E:\MyProject\WLZJ_Update\04_JieKou\WindowsService_1709_180704\bin\Debug\WindowsService_1709_180704.exe 程序集的进度。
该文件位于 E:\MyProject\WLZJ_Update\04_JieKou\WindowsService_1709_180704\bin\Debug\WindowsService_1709_180704.InstallLog。
正在卸载程序集“E:\MyProject\WLZJ_Update\04_JieKou\WindowsService_1709_180704\bin\Debug\WindowsService_1709_180704.exe”。
受影响的参数是:
   logtoconsole =
   assemblypath = E:\MyProject\WLZJ_Update\04_JieKou\WindowsService_1709_180704\bin\Debug\WindowsService_1709_180704.exe
   logfile = E:\MyProject\WLZJ_Update\04_JieKou\WindowsService_1709_180704\bin\Debug\WindowsService_1709_180704.InstallLog
正在移除 EventLog 源 Service6。
警告: 源 Service6 未在本地计算机上注册。
正在从系统中移除服务 Service6...
在卸载 System.ServiceProcess.ServiceInstaller 安装程序的过程中发生异常。
System.ComponentModel.Win32Exception: 指定的服务未安装。
在卸载的过程中发生异常。将忽略该异常并继续卸载。但是,在卸载完成之后应用程序可能未完全卸载。
正在移除 EventLog 源 Service5。
警告: 源 Service5 未在本地计算机上注册。
正在从系统中移除服务 Service5...
在卸载 System.ServiceProcess.ServiceInstaller 安装程序的过程中发生异常。
System.ComponentModel.Win32Exception: 指定的服务未安装。
在卸载的过程中发生异常。将忽略该异常并继续卸载。但是,在卸载完成之后应用程序可能未完全卸载。
正在移除 EventLog 源 Service4。
正在从系统中移除服务 Service4...
已成功地从系统中移除服务 Service4。
正在移除 EventLog 源 Service3。
正在从系统中移除服务 Service3...
已成功地从系统中移除服务 Service3。
正在移除 EventLog 源 Service2。
正在从系统中移除服务 Service2...
已成功地从系统中移除服务 Service2。
正在移除 EventLog 源 Service1。
正在从系统中移除服务 Service1...
已成功地从系统中移除服务 Service1。

卸载完成。
在卸载的过程中发生异常。将忽略该异常并继续卸载。但是,在卸载完成之后应用程序可能未完全卸载。

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>

注:Service1至Service4是我之前安装过的,Service5和Service6是我后开发的,添加了安装程序但并没有安装服务。

推荐阅读