c#定时服务使用bat命令运行命令启动文件抛出异常

问题遇到的现象和发生背景

我开发了一个定时服务,在部署的时候要用到bat命令去运行,现在运行的时候抛出异常了。

遇到的现象和发生背景,请写出第一个错误信息
“安装”阶段已成功完成,正在开始“提交”阶段。
查看日志文件的内容以获得 D:\VIPSService\01_工程区\02-Sprint迭代\01_迭代1\03_构建集成\V1.0.04\VIPSToQmsService\VIPSToQmsService\bin\Debug\VIPSToQmsService.exe 程序集的进度。
该文件位于 D:\VIPSService\01_工程区\02-Sprint迭代\01_迭代1\03_构建集成\V1.0.04\VIPSToQmsService\VIPSToQmsService\bin\Debug\VIPSToQmsService.InstallLog。
正在提交程序集“D:\VIPSService\01_工程区\02-Sprint迭代\01_迭代1\03_构建集成\V1.0.04\VIPSToQmsService\VIPSToQmsService\bin\Debug\VIPSToQmsService.exe”。
受影响的参数是:
   logtoconsole =
   logfile = D:\VIPSService\01_工程区\02-Sprint迭代\01_迭代1\03_构建集成\V1.0.04\VIPSToQmsService\VIPSToQmsService\bin\Debug\VIPSToQmsService.InstallLog
   assemblypath = D:\VIPSService\01_工程区\02-Sprint迭代\01_迭代1\03_构建集成\V1.0.04\VIPSToQmsService\VIPSToQmsService\bin\Debug\VIPSToQmsService.exe
没有 RunInstallerAttribute.Yes 的公共安装程序。在 D:\VIPSService\01_工程区\02-Sprint迭代\01_迭代1\03_构建集成\V1.0.04\VIPSToQmsService\VIPSToQmsService\bin\Debug\VIPSToQmsService.exe 程序集中应该可以找到“Yes”特性。
没有安装程序,因此移除 InstallState 文件。
“提交”阶段已成功完成。
已完成事务处理安装。
D:\VIPSService\01_工程区\02-Sprint迭代\01_迭代1\03_构建集成\V1.0.04\VIPSToQmsService\VIPSToQmsService\bin\Debug>net start VIPSToQmsService
服务名无效。
请键入 NET HELPMSG 2185 以获得更多的帮助。
D:\VIPSService\01_工程区\02-Sprint迭代\01_迭代1\03_构建集成\V1.0.04\VIPSToQmsService\VIPSToQmsService\bin\Debug>pause
请按任意键继续. . .

用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
启动命令:
InstallUtil.exe VIPSToQmsService.exe /u
InstallUtil.exe VIPSToQmsService.exe
net start VIPSToQmsService 
pause
来源文件:
WinServiceSetup.bat
运行结果及详细报错内容

img

img

img

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%

我那个位置配置的不对

我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”

安装服务成功

没明白bat是干什么的
既然你说你开发了一个服务,那你的服务就应该在系统的服务列表里,应该在服务里启动,而不是什么运行bat文件
如果只是定时执行一段代码,写个计划任务就行了,搞个服务没事死循环sleep是不明智的,除非你这个定时时间非常短,比如每5秒就要执行一次

根据你提供的信息,你在部署定时服务时遇到了“没有 RunInstallerAttribute.Yes 的公共安装程序”的异常。

这个错误的原因是:你的定时服务没有实现 System.Configuration.Install.Installer 类,也就是说没有重写 Install 方法,无法进行安装。

解决方法:

在定时服务的类中重写 Install 方法,实现定时服务的安装功能。
在类的定义中添加 [RunInstaller(true)] 特性,表示这个类是一个安装程序。
具体可以参考以下代码:

[RunInstaller(true)]
public class MyServiceInstaller : System.Configuration.Install.Installer
{
    private ServiceInstaller serviceInstaller;
    private ServiceProcessInstaller processInstaller;

    public MyServiceInstaller()
    {
        // 实例化自定义的安装程序
        processInstaller = new ServiceProcessInstaller();
        serviceInstaller = new ServiceInstaller();

        // 设置安装程序的属性
        processInstaller.Account = ServiceAccount.LocalSystem;
        serviceInstaller.StartType = ServiceStartMode.Automatic;
        serviceInstaller.ServiceName = "MyService";

        // 将安装程序添加到安装程序集合中
        Installers.Add(serviceInstaller);
        Installers.Add(processInstaller);
    }
}