1. 使用C#.NET开发Windows服务并安装部署

2. 1 Windows 服务开发

通过查询网上资料,本文主要参考文章Create A Windows Service In C#[1]进行安装。但是文章中仍然缺少一些细节,而导致的踩坑。因此,本文对这些内容进行记录。

  • 需要在 serviceProcessInstaller1 的属性中,需要将 Account 设置为 LocalSystem,这样才会有运行的权限;
  • 服务程序的目录结构不同于常规的Windows程序,只能通过 AppDomain.CurrentDomain.BaseDirectory 来获得。

3. 2 关于.NET中的Service

  1. 服务的本质就是自动运行指定程序,系统以服务的形式,能够手动或自动地启动指定的程序;
  2. 服务程序虽然也是以exe后缀的,但是不是一般程序,无法双击直接运行;
  3. 一般程序也无法当作服务程序来使用,这是因为服务程序必需派生于ServiceBase类,以使系统调用;
  4. 服务与窗体类型相同,只要服务不停止,程序的线程就一直处于运行中的状态,可以执行各类任务;

4. 3 安装服务

服务的安装实际上就是将编译好的exe服务文件加载到系统的服务中,具体包括以下两步。

4.1. 3.1 安装命令 InstallUtil.exe

使用服务安装使用 InstallUtil.exe 程序,命令格式为:InstallUtil.exe [服务程序路径],其中 InstallUtil.exe 有32位和64位两个版本,位置为:

  • 32位的x86版:C:\Windows\Microsoft.NET\Framework\v4.0.30319
  • 64位的x64版:C:\Windows\Microsoft.NET\Framework64\v4.0.30319

[服务程序路径]C:\Services\FileUploadService.exe, 则命令为: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe C:\Services\FileUploadService.exe

注意:如果不存在InstallUtil的目录,则需要安装 .net framework,

4.2. 3.2 安装脚本

为方便安装,编写了 ServiceInstall.bat 脚本,内容为: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe FileUploadService.exe

注意事项:一定要使用系统管理员权限启动命令行才可以,否则会因权限不足而失败。

5. 4 启动服务

  1. Win键+R 启动“运行”;
  2. 输入 services.msc 启动Windows服务管理窗口;
  3. 找到 FileUploadService 服务,然后启动。

6. 5 注意事项

安装和运行需要请注意以下内容:

  • 启动 cmd 需要使用管理员权限;
  • 当服务更新后,如果函数结构关系没有关键性的变化,只需停止服务后替换服务程序的可执行文件即可。

7. 安装记录

7.1. 2023/09/06

  • 安装
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe
  • 反安装
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe -U C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe

执行过程中出现以下错误

PS C:\Apps\TopmostWindowsMonitor> C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe .\TopmostWindowMonitor.exe
Microsoft (R) .NET Framework 安装实用工具版本 4.8.9032.0
版权所有 (C) Microsoft Corporation。保留所有权利。


正在运行事务处理安装。

正在开始安装的“安装”阶段。
查看日志文件的内容以获得 C:\Apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe 程序集的进度。
该文件位于 C:\Apps\TopmostWindowsMonitor\TopmostWindowMonitor.InstallLog。
正在安装程序集“C:\Apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe”。
受影响的参数是:
   logtoconsole =
   assemblypath = C:\Apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe
   logfile = C:\Apps\TopmostWindowsMonitor\TopmostWindowMonitor.InstallLog
正在安装服务 Service1...
正在日志 Application 中创建 EventLog 源 Service1...

在“安装”阶段发生异常。
System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。  不可访问的日志: Security, State。

正在开始安装的“回退”阶段。
查看日志文件的内容以获得 C:\Apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe 程序集的进度。
该文件位于 C:\Apps\TopmostWindowsMonitor\TopmostWindowMonitor.InstallLog。
正在回滚程序集“C:\Apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe”。
受影响的参数是:
   logtoconsole =
   assemblypath = C:\Apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe
   logfile = C:\Apps\TopmostWindowsMonitor\TopmostWindowMonitor.InstallLog
正在将事件日志还原到源 Service1 的前一状态。
在 System.Diagnostics.EventLogInstaller 安装程序的“回退”阶段发生异常。
System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。  不可访问的日志: Security, State。
在安装的“回退”阶段发生异常。将忽略该异常并继续回退。但是,在完成回退后计算机可能无法完全还原到它的初始状态。

“回退”阶段已成功完成。

已完成事务处理安装。
安装失败,已执行回退。

解决方案:使用系统管理员权限启动命令行

PS C:\Windows\System32> C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe
Microsoft (R) .NET Framework 安装实用工具版本 4.8.9032.0
版权所有 (C) Microsoft Corporation。保留所有权利。


正在运行事务处理安装。

正在开始安装的“安装”阶段。
查看日志文件的内容以获得 C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe 程序集的进度。
该文件位于 C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.InstallLog。
正在安装程序集“C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe”。
受影响的参数是:
   logtoconsole =
   assemblypath = C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe
   logfile = C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.InstallLog
正在安装服务 Service1...
已成功安装服务 Service1。
正在日志 Application 中创建 EventLog 源 Service1...

“安装”阶段已成功完成,正在开始“提交”阶段。
查看日志文件的内容以获得 C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe 程序集的进度。
该文件位于 C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.InstallLog。
正在提交程序集“C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe”。
受影响的参数是:
   logtoconsole =
   assemblypath = C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.exe
   logfile = C:\apps\TopmostWindowsMonitor\TopmostWindowMonitor.InstallLog

“提交”阶段已成功完成。

已完成事务处理安装。

8. 6 参考资料

[1] Create A Windows Service In C#, https://www.c-sharpcorner.com/article/create-windows-services-in-c-sharp/ [2] 使用C#创建Windows服务, https://www.cnblogs.com/cncc/p/7170951.html [3] C#编写Windows服务程序图文教程 http://www.taodudu.cc/news/show-404406.html?action=onClick 内容完善,推荐阅读。

results matching ""

    No results matching ""