C# Windows 服务未显示在已安装的服务中(因此无法启动)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10716333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:57:28  来源:igfitidea点击:

Windows service not showing up in installed services (and hence unable to start)

c#windows-services

提问by user1240679

I just built a very simple Windows service to test something and built it to get the .exe. As per this articlein MSDN, I also installed the service using the Visual Studio Command Prompt 2010 (ran as administrator) installutil TestService.exe.

我刚刚构建了一个非常简单的 Windows 服务来测试某些东西并构建它来获取 .exe。根据MSDN 中的这篇文章,我还使用 Visual Studio 命令提示符 2010(以管理员身份运行)安装了该服务installutil TestService.exe

When using the installutilcommand, everything runs fine and I get a message saying that the service has been installed. Further, to start the service when I check inside the Services node (in Server Explorer in Visual Studio), I do not see any service named TestService.exe.

使用该installutil命令时,一切正常,我收到一条消息,说该服务已安装。此外,为了在我检查服务节点内部(在 Visual Studio 的服务器资源管理器中)时启动服务,我没有看到任何名为TestService.exe.

Also, Start Menu -> My Computer (Right Click) -> Manage -> Services and Application -> Services does not show the TestService.exethrough which I could start the service.

此外,开始菜单 -> 我的电脑(右键单击) -> 管理 -> 服务和应用程序 -> 服务没有显示TestService.exe我可以通过它启动服务。

Any suggestions how do I start the service?

任何建议如何启动服务?

Code inside OnStartfunction:

OnStart函数内部代码:

Process[] testProcess = Process.GetProcessesByName("notepad.exe");
if (testProcess.Length == 0)
    File.WriteAllText(@"C:\Users\User1\Desktop\service.txt", "nothing");

else
    File.WriteAllText(@"C:\Users\User1\Desktop\service.txt", "run");

采纳答案by Bali C

I have had problems with installutilbefore and found using the sccommand works whereas installutildidn't.

我以前遇到过问题,installutil发现使用sc命令有效,而installutil没有。

Try installing your service with this command:

尝试使用以下命令安装您的服务:

sc create servicename binPath= serviceexe.exe

回答by Tyshun

I just had this issue and was able to resolve it with the following steps. If you skip these steps, your setup project will build and copy your files to the correct directory; however, they will not register your binary as a service without these steps.

我刚刚遇到了这个问题,并且能够通过以下步骤解决它。如果您跳过这些步骤,您的安装项目将构建并将您的文件复制到正确的目录;但是,如果没有这些步骤,他们不会将您的二进制文件注册为服务。

  1. In Solution Explorer, right-click the setup project, point to View, then choose Custom Actions. The Custom Actions editor appears.

  2. In the Custom Actions editor, right-click the Custom Actions node and choose Add Custom Action. The Select Item in Project dialog box appears.

  3. Double-click the application folder in the list box to open it, select primary output from MyNewService (Active), and click OK. The primary output is added to all four nodes of the custom actions: Install, Commit, Rollback, and Uninstall.

  4. Build the setup project.

  1. 在解决方案资源管理器中,右键单击安装项目,指向查看,然后选择自定义操作。出现自定义操作编辑器。

  2. 在自定义操作编辑器中,右键单击自定义操作节点并选择添加自定义操作。出现“在项目中选择项目”对话框。

  3. 双击列表框中的应用程序文件夹将其打开,从 MyNewService (Active) 中选择主输出,然后单击确定。主要输出将添加到自定义操作的所有四个节点:安装、提交、回滚和卸载。

  4. 构建安装项目。

回答by JabberwockyDecompiler

While working in VS 2013, I ran into this issue when I added the installer by right clicking on the project and adding a new item of type Installer Class. The key difference is that you need to right click on the Design Viewof the Windows Serviceand click Add Installer.

在 VS 2013 中工作时,当我通过右键单击项目并添加类型的新项目来添加安装程序时遇到了这个问题Installer Class。关键区别在于您需要右键单击Design ViewWindows Service并单击Add Installer

For full details on .Net 4.5 and 4.6 see Creating a service, Adding Installers, and Installing a Service

有关 .Net 4.5 和 4.6 的完整详细信息,请参阅创建服务添加安装程序安装服务