在Visual Studio中创建的Windows服务安装

148

当我在Visual Studio 2010中创建一个新的Windows服务时,我会收到一条消息,提示我使用InstallUtil和net start来运行该服务。

我尝试了以下步骤:

  1. 创建新项目:文件 -> 新建 -> 项目 -> Windows服务
  2. 项目名称:TestService
  3. 按原样构建项目(包括Service1构造函数、OnStart、OnStop)
  4. 打开命令提示符,运行"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" TestService.exe
  5. 运行net start TestService

第4步的输出结果

运行事务性安装。 开始安装阶段。 查看日志文件以了解 C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe 组件的进度。 文件位于 C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.InstallLog。 正在安装组件 'C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe'。 受影响参数包括: logtoconsole = logfile = C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.InstallLog assemblypath = C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe。 在 C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe 组件中找不到带有 RunInstallerAttribute.Yes 属性的公共安装程序。 安装阶段已成功完成,并开始提交阶段。 查看日志文件以了解 C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe 组件的进度。 文件位于 C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.InstallLog。 正在提交组件 'C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe'。 受影响参数包括: logtoconsole = logfile = C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.InstallLog assemblypath = C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe。 在 C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe 组件中找不到带有 RunInstallerAttribute.Yes 属性的公共安装程序。 删除 InstallState 文件,因为没有安装程序。 提交阶段已成功完成。 事务性安装已完成。

服务名称无效。

键入 NET HELPMSG 2185 可获取更多帮助信息。

8个回答

260
您需要在设计器中打开Service.cs文件,右键单击它并选择菜单选项“添加安装程序”。
它不会直接安装... 您需要先创建安装程序类。
一些关于服务安装程序的参考: 如何向您的服务应用程序添加安装程序 相当古老... 但这就是我所说的: C#中的Windows服务:添加安装程序(第3部分) 通过这样做,将自动创建一个ProjectInstaller.cs。 然后,您可以双击此文件,进入设计器,并配置组件:
- serviceInstaller1具有服务本身的属性:Description、DisplayName、ServiceName和StartType是最重要的。 - serviceProcessInstaller1具有此重要属性:Account,它是服务运行的帐户。
例如:
this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;

3
加入安装程序并将账户设置为LocalSystem就解决了。谢谢! - Luc
1
我在VS2013中遇到了相同的错误。我检查了您提供的链接,验证了我已经正确配置了ProjectInstaller,包括组件service[Process]Installer1。我以管理员身份运行了installutil.exe。它仍然报告“找不到具有RunInstallerAttribute.Yes属性的公共安装程序”。有任何想法吗? - Barry Dysert
5
哈哈,我喜欢“相当古老”的链接指向一个名为Arcane Code的网站。随着那个页面越来越旧,它的名字就变得越来越贴切 :-) - HotN
什么是“设计师”?通常没有用户界面的应用程序不涉及任何被称为设计师的东西。 - Maxx
服务也有设计师,就像表单一样。 - Grungondola

12

看起来您的代码中可能没有安装程序类,即继承自 Installer 的类,该类将告诉 installutil 如何将可执行文件安装为服务。在 C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestSe rvice\obj\x86\Debug\TestService.exe 程序集中找不到带有 RunInstallerAttribute.Yes 特性的公共安装程序。

另外,我有一个自己的小型自安装/调试 Windows 服务模板,您可以从中复制代码或使用:可调试、自安装的 Windows 服务


当我在Visual Studio中右键单击TestService项目->属性->服务时,该页面被禁用了...您是指不同的位置吗?在应用程序下,程序集名称为TestService。 - jkh
@John:忽略有关服务控制台的第一部分,看看以“实际上”开头的第二部分。看起来服务从未安装,因为它没有找到安装程序。 - James Michael Hare

10

这里有一种替代方式可以制作安装程序并消除错误信息。此外,似乎VS2015 Express没有“添加安装程序”菜单项。

您只需要创建一个类,并添加以下代码,然后添加System.Configuration.Install.dll引用即可。

using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;


namespace SAS
{
    [RunInstaller(true)]
    public class MyProjectInstaller : Installer
    {
        private ServiceInstaller serviceInstaller1;
        private ServiceProcessInstaller processInstaller;

        public MyProjectInstaller()
        {
            // Instantiate installer for process and service.
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller1 = new ServiceInstaller();

            // The service runs under the system account.
            processInstaller.Account = ServiceAccount.LocalSystem;

            // The service is started manually.
            serviceInstaller1.StartType = ServiceStartMode.Manual;

            // ServiceName must equal those on ServiceBase derived classes.
            serviceInstaller1.ServiceName = "SAS Service";

            // Add installer to collection. Order is not important if more than one service.
            Installers.Add(serviceInstaller1);
            Installers.Add(processInstaller);
        }
    }
}

同时运行VS2015,这个解决方案让我摆脱了之前遇到的“没有带有RunInstallerAttribute.Yes的公共安装程序”错误信息。谢谢! - PHBeagle

9

两个典型问题:

  1. 缺少ProjectInstaller类(就像@MiguelAngelo所指出的那样)
  2. 命令提示符必须“以管理员身份运行

4
另一个可能的问题是(我遇到了):确保ProjectInstaller类是public。老实说,我不确定我是如何做到的,但我在ProjectInstaller.Designer.cs中添加了事件处理程序,例如:this.serviceProcessInstaller1.BeforeInstall += new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1_BeforeInstall);。我猜在自动创建ProjectInstaller.cs中的处理函数的过程中,它将类定义从public class ProjectInstaller : System.Configuration.Install.Installer更改为partial class ProjectInstaller : System.Configuration.Install.Installer,用partial关键字替换public关键字。所以,为了修复它必须这样:public partial class ProjectInstaller : System.Configuration.Install.Installer。我使用的是Visual Studio 2013社区版。

我知道这已经是三年后的事了,但这解决了我的问题。不确定为什么或何时设计师将之前的公共部分类更改为内部部分类。谢谢! - trashrobber

2

Visual Studio 2010 和 .NET 4.0 及之后版本中的隐秘更改

找不到带有 RunInstallerAttribute.Yes 属性的公共安装程序

在 .NET 中存在别名更改或编译器清理,这可能会揭示针对您特定情况的小调整。

如果您有以下代码...

RunInstaller(true)   // old alias  

您可能需要更新它。
RunInstallerAttribute(true)  // new property spelling

这就像在编译时或运行时更改别名,您将获得此错误行为。上述显式更改为RunInstallerAttribute(true) 在所有机器上的所有安装方案中都已解决。

添加项目或服务安装程序后,请检查“旧”的RunInstaller(true),并将其更改为新的RunInstallerAttribute(true)


根据我的了解,您可以省略“Attribute”后缀,即[RunInstaller(true)]和[RunInstallerAttribute(true)]是相同的。尝试使用不同的属性,例如[DebuggerStepThrough()]和[DebuggerStepThroughAttribute()] - 当应用于类时,两者都可以正常工作。 - Matt

1

我遇到了另一个问题:确保你的安装程序派生类(通常是ProjectInstaller)位于命名空间层次结构的顶部,我试图在另一个公共类中使用公共类,但这会导致相同的错误:

找不到带有RunInstallerAttribute.Yes属性的公共安装程序


0
或者只需使用sc.exe来添加(创建开关)或删除(删除开关)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接