2011-10-27 45 views
112

当我在Visual Studio 2010中创建一个新的Windows服务时,我收到消息指出要使用InstallUtil和net start来运行该服务。安装在Visual Studio中创建的Windows服务

我曾尝试以下步骤:

  1. 创建新的项目文件 - >新建 - >项目 - > Windows服务
  2. 项目名称:TestService的
  3. 生成项目作为为(服务1构造,OnStart中,调用OnStop)
  4. 打开命令提示符,运行“C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \ InstallUtil.exe” TestService.exe
  5. 运行net start TestService

步骤4

的输出运行事务处理安装。

开始安装的安装阶段。

查看日志文件的内容为 C:\ Users \用户名为myusername \文档\ Visual Studio的 2010 \项目\ TestService的\ TestService的\ OBJ \ 86 \调试\ TestService.exe 集的进度。

该文件位于C:\ Users \ myusername \ Documents \ Visual Studio 2010 \ Projects \ Tes tService \ TestService \ obj \ x86 \ Debug \ TestService.InstallLog。

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

受影响的参数是:

logtoconsole =

日志文件= C:\ Users \用户名为myUsername \文档\ Visual Studio中 2010 \项目\ TestService的\ t estService \ OBJ \ 86 \调试\ TestService的。 InstallLog

assemblypath = C:\ Users \用户名为myUsername \文件\的Visual Studio 2010 \项目\ TestServ冰\ TestService的\ OBJ \ 86 \调试\ TestService.exe

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

安装阶段成功完成,提交阶段开始时为 。

查看日志文件的内容为 C:\ Users \用户名为myusername \文档\ Visual Studio的 2010 \项目\ TestService的\ TestService的\ OBJ \ 86 \调试\ TestService.exe 集的进度。

该文件位于C:\ Users \ myusername \ Documents \ Visual Studio 2010 \ Projects \ Tes tService \ TestService \ obj \ x86 \ Debug \ TestService.InstallLog。

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

受影响的参数是:

logtoconsole =

日志文件= C:\ Users \用户名为myUsername \文档\ Visual Studio中 2010 \项目\ TestService的\ t estService \ OBJ \ 86 \调试\ TestService的。 InstallLog

assemblypath = C:\ Users \用户名为myUsername \文件\的Visual Studio 2010 \项目\ TestServ冰\ TestService的\ OBJ \ 86 \调试\ TestService.exe

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

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

提交阶段已成功完成。

交易安装已完成。

步骤5

的输出的服务名称是无效的。

更多的帮助。通过键入NET HELPMSG 2185

回答

209

您需要在设计器中打开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 thas是服务将运行的帐户。

例如:

this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem; 
+3

添加安装程序并将帐户设置为LocalSystem。谢谢! – Luc

+0

我在VS2013中遇到同样的错误。我检查了你提供的链接,确认我有ProjectInstaller,包括正确配置的组件服务[Process] Installer1。我以管理员身份运行installutil.exe。它仍然会报告“没有公共安装程序可以找到RunInstallerAttribute.Yes属性”。有任何想法吗? –

+4

哈哈。我喜欢“古老的”链接是一个名为“奥术法典”的网站。页面越老,名称变得越真实:-) – HotN

10

综观:

与RunInstallerAttribute.Yes没有公开的安装属性可以在C中找到:\ Users \用户名为myusername \文件\ Visual Studio 2010 \ Projects \ TestService \ TestSevice \ obj \ x86 \ Debug \ TestService.exe程序集。

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

P.s.我在这里有我自己的一个自我安装/可调试的Windows服务模板,您可以从这里复制代码或使用:Debuggable, Self-Installing Windows Service

+0

我用鼠标右键单击在Visual Studio中TestService的项目 - >属性 - >服务,该页面被禁用...你指的是不同的位置?在应用程序下,程序集名称是TestService。 – jkh

+0

@John:忽略关于服务控制台的第一部分,看看以*实际*开头的第二部分。它看起来像从未安装的服务,因为它没有找到安装程序。 –

2

两种典型问题:

  1. 缺少的ProjectInstaller类(如@MiguelAngelo指出)
  2. 命令提示必须“作为运行管理员
2

另一个可能的问题(这是我碰到了):

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

6

这是使安装程序和摆脱该错误消息的替代方法。此外,似乎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); 
     } 
    } 
} 
+0

运行VS2015以及此解决方案让我通过了以前得到的“没有公开安装程序与RunInstallerAttribute.Yes”错误消息。谢谢! – PHBeagle

1

隐身变化在VS 2010和.NET 4.0及更高版本

与RunInstallerAttribute.Yes没有公共属性的安装程序可以找到

有一个别名变化或编译器清理在.NET中,可能会显示您的具体情况的这个小调整。

如果你有以下代码...

RunInstaller(true) // old alias 

您可能需要将其更新到

RunInstallerAttribute(true) // new property spelling 

它就像一个别名在幕后改变在编译时或运行时,你会得到这个错误行为。以上对RunInstallerAttribute的显式更改(true)已将其固定在所有机器上的所有安装方案中。

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

+0

据我所知,您可以省略“属性”后缀,即[RunInstaller(true)]和[RunInstallerAttribute(true)]是相同的。尝试使用不同的属性,例如[DebuggerStepThrough()]和[DebuggerStepThroughAttribute()] - 在应用于类时都可以使用。 – Matt

相关问题