【问题标题】:Install Windows Service created in Visual Studio安装在 Visual Studio 中创建的 Windows 服务
【发布时间】:2011-12-16 20:25:43
【问题描述】:

当我在 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\项目\特斯 tService\TestService\obj\x86\Debug\TestService.InstallLog.

安装程序集 'C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe'。

受影响的参数有:

logtoconsole =

日志文件 = C:\Users\myusername\Documents\Visual Studio 2010\项目\测试服务\T estService\obj\x86\Debug\TestService.InstallLog

程序集路径 = 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.exe 组装进度。

文件位于 C:\Users\myusername\Documents\Visual Studio 2010\项目\特斯 tService\TestService\obj\x86\Debug\TestService.InstallLog.

提交程序集 'C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe'。

受影响的参数有:

logtoconsole =

日志文件 = C:\Users\myusername\Documents\Visual Studio 2010\项目\测试服务\T estService\obj\x86\Debug\TestService.InstallLog

程序集路径 = 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 组装。

删除 InstallState 文件,因为没有安装程序。

提交阶段成功完成。

事务安装已完成。

第 5 步的输出

服务名称无效。

键入 NET HELPMSG 2185 可获得更多帮助。

【问题讨论】:

    标签: c# windows service windows-services installutil


    【解决方案1】:

    看着:

    在 C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe 程序集中找不到具有 RunInstallerAttribute.Yes 属性的公共安装程序。

    您的代码中似乎没有安装程序类。这是一个继承自 Installer 的类,它将告诉 installutil 如何将您的可执行文件安装为服务。

    附:我有自己的小型自安装/可调试 Windows 服务模板,您可以从中复制代码或使用它:Debuggable, Self-Installing Windows Service

    【讨论】:

    • 当我在 Visual Studio 中右键单击 TestService 项目 -> 属性 -> 服务时,页面被禁用...您指的是不同的位置吗?在 Application 下,程序集名称为 TestService。
    • @John:关于服务控制台的第一部分忽略,看以Actually开头的第二部分。看起来该服务从未安装,因为它没有找到安装程序。
    【解决方案2】:

    您需要在设计器中打开Service.cs文件,右键单击它并选择菜单选项“添加安装程序”。

    它不会立即安装...您需要先创建安装程序类。

    有关服务安装程序的一些参考:

    How to: Add Installers to Your Service Application

    相当老了……但这就是我要说的:

    Windows Services in C#: Adding the Installer (part 3)

    通过这样做,ProjectInstaller.cs 将被自动创建。然后你可以双击这个,进入设计器,配置组件:

    • serviceInstaller1具有服务本身的属性:DescriptionDisplayNameServiceNameStartType是最重要的。

    • serviceProcessInstaller1 具有以下重要属性:Account,即运行服务的帐户。

    例如:

    this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
    

    【讨论】:

    • 添加安装程序并将帐户设置为 LocalSystem 做到了。谢谢!
    • 我在 VS2013 中遇到了同样的错误。我检查了您提供的链接,确认我有正确配置的 ProjectInstaller,包括组件 service[Process]Installer1。我以管理员身份运行 installutil.exe。它仍然报告“找不到具有 RunInstallerAttribute.Yes 属性的公共安装程序”。有什么想法吗?
    • 大声笑。我喜欢“相当古老”的链接指向一个名为 Arcane Code 的网站。该页面越旧,名称就越真实:-)
    • 什么是“设计师”?没有 UI 的应用程序通常不涉及任何可以称为设计器的东西。
    • 服务也有设计师,就像表单一样
    【解决方案3】:

    两个典型问题:

    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 社区版。

      【讨论】:

      • 我知道这是 3 年后的事了,但这解决了我的问题。不知道设计者为什么或何时将以前的公共部分类更改为内部部分类。谢谢!
      【解决方案5】:

      这是制作安装程序并消除该错误消息的另一种方法。另外,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 的公共安装程序”的错误消息。谢谢!
      【解决方案6】:

      VS 2010 和 .NET 4.0 及更高版本中的隐形变化

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

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

      如果你有以下代码……

      RunInstaller(true)   // old alias  
      

      您可能需要将其更新为

      RunInstallerAttribute(true)  // new property spelling
      

      这就像在编译时或运行时在幕后更改的别名,您将得到此错误行为。上面对 RunInstallerAttribute(true) 的显式更改在我们所有机器上的所有安装场景中都修复了它。

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

      【讨论】:

      • 据我所知,您可以省略“属性”后缀,即 [RunInstaller(true)] 和 [RunInstallerAttribute(true)] 是相同的。尝试使用不同的属性,例如[DebuggerStepThrough()] 和 [DebuggerStepThroughAttribute()] - 应用于类时都可以工作。
      【解决方案7】:

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

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

      【讨论】:

        猜你喜欢
        • 2012-09-17
        • 1970-01-01
        • 1970-01-01
        • 2012-06-05
        • 2012-02-19
        • 1970-01-01
        • 1970-01-01
        • 2011-03-10
        • 2014-08-29
        相关资源
        最近更新 更多