C# 无法从命令行或调试器启动服务,必须首先安装Windows服务

C# 2020-02-27 阅读 1506 评论 0

参考C#官方教程:创建 Windows 服务应用,搭建windows service项目。

问题的出现

使用 Visual Studio 2019 创建 C# windows 服务项目,尝试运行.exe的时候,出现错误:

无法从命令行或调试器启动服务,必须首先安装Windows服务(使用installutil.exe),然后用ServerExplorer、Windows服务器管理工具或NET START命令启动它。

Cannot start service from the command line or debugger. A winwows Service must first be installed(using installutil.exe) and then started with the ServerExplorer, Windows Services Afministrative tool or the NET START command.

使用 Installutil.exe 安装和卸载服务

安装服务

installutil 的安装命令:

installutil <yourproject>.exe

实用工具 installutil.exe 一般安装在 %WINDIR%\Microsoft.NET\Framework[64]\ 中,如我的电脑是在C:\Windows\Microsoft.NET\Framework\v4.0.30319,安装服务如下:

C:\Windows\Microsoft.NET\Framework\v4.0.30319>InstallUtil.exe D:\TestService1\TestService1\bin\Debug\TestService1.exe
Microsoft (R) .NET Framework 安装实用工具版本 4.8.3761.0
版权所有 (C) Microsoft Corporation。保留所有权利。


正在运行事务处理安装。

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

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

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

已完成事务处理安装。

卸载服务

如果想卸载已有的 windows 服务,使用命令:

installutil /u <yourproject>.exe

如我卸载了这个服务:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe /u D:\TestService1\TestService1\bin\Debug\TestService1.exe
Microsoft (R) .NET Framework 安装实用工具版本 4.8.3761.0
版权所有 (C) Microsoft Corporation。保留所有权利。



正在开始卸载。
查看日志文件的内容以获得 D:\TestService1\TestService1\bin\Debug\TestService1.exe 程序集的进度 。
该文件位于 D:\TestService1\TestService1\bin\Debug\TestService1.InstallLog。
正在卸载程序集“D:\TestService1\TestService1\bin\Debug\TestService1.exe”。
受影响的参数是:
   logtoconsole =
   assemblypath = D:\TestService1\TestService1\bin\Debug\TestService1.exe
   logfile = D:\TestService1\TestService1\bin\Debug\TestService1.InstallLog
正在移除 EventLog 源 MyNewService。
正在从系统中移除服务 MyNewService...
已成功地从系统中移除服务 MyNewService。

卸载完成。

启动windows服务

使用 net start 命令

net start ServiceName

如:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>net start MyNewService
cpming的服务 服务正在启动 .
cpming的服务 服务已经启动成功。

在服务控制台启动

控制面板 -> 管理工具 -> 服务,在服务(本地)找到相关的服务,鼠标右键的菜单选择启动。

Visual Studio 调试服务

参考官网的文档

https://docs.microsoft.com/zh-cn/dotnet/framework/windows-services/how-to-debug-windows-service-applications

最后更新 2020-06-18
MIP.watch('startSearch', function (newVal, oldVal) { if(newVal) { var keyword = MIP.getData('keyword'); console.log(keyword); // 替换当前历史记录,新增 MIP.viewer.open('/s/' + keyword, {replace: true}); setTimeout(function () { MIP.setData({startSearch: false}) }, 1000); } }); MIP.watch('goHome', function (newVal, oldVal) { MIP.viewer.open('/', {replace: false}); });