143

当我在 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”。

受影响的参数是:

登录控制台 =

日志文件 = 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

在 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”。

受影响的参数是:

登录控制台 =

日志文件 = 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

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

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

提交阶段成功完成。

事务安装已完成。

步骤 5 的输出

服务名称无效。

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

4

7 回答 7

252

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

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

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

如何:将安装程序添加到您的服务应用程序

相当老......但这就是我所说的:

C# 中的 Windows 服务:添加安装程序(第 3 部分)

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

  • serviceInstaller1具有服务本身的属性:、 和DescriptionDisplayName最重要的。ServiceNameStartType

  • serviceProcessInstaller1有一个重要的属性:Account即服务将在其中运行的帐户。

例如:

this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
于 2011-10-27T20:45:56.440 回答
12

看着:

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

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

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

于 2011-10-27T20:45:18.797 回答
9

这是制作安装程序并消除该错误消息的另一种方法。此外,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);
        }
    }
}
于 2016-11-03T19:48:48.107 回答
7

两个典型问题:

  1. 缺少 ProjectInstaller 类(正如@MiguelAngelo 指出的那样)
  2. 命令提示符必须“以管理员身份运行” < /li>
于 2015-08-29T21:38:29.630 回答
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

public将关键字替换partial. 所以,为了修复它,它必须是

public partial class ProjectInstaller : System.Configuration.Install.Installer

我使用 Visual Studio 2013 社区版。

于 2016-07-28T12:18:07.920 回答
2

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

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

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

如果您有以下代码...

RunInstaller(true)   // old alias  

您可能需要将其更新为

RunInstallerAttribute(true)  // new property spelling

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

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

于 2017-01-22T18:02:23.830 回答
1

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

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

于 2019-03-26T14:09:32.257 回答