Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 安装在Visual Studio中创建的Windows服务_C#_Windows_Service_Windows Services_Installutil - Fatal编程技术网

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

C# 安装在Visual Studio中创建的Windows服务,c#,windows,service,windows-services,installutil,C#,Windows,Service,Windows Services,Installutil,当我在VisualStudio2010中创建一个新的Windows服务时,我收到一条消息,说明要使用InstallUtil和net start来运行该服务 我尝试了以下步骤: 创建新项目文件->新建->项目->Windows服务 项目名称:TestService 按原样构建项目(Service1构造函数、OnStart、OnStop) 打开命令提示符,运行“C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe”TestServ

当我在VisualStudio2010中创建一个新的Windows服务时,我收到一条消息,说明要使用InstallUtil和net start来运行该服务

我尝试了以下步骤:

  • 创建新项目文件->新建->项目->Windows服务
  • 项目名称:TestService
  • 按原样构建项目(Service1构造函数、OnStart、OnStop)
  • 打开命令提示符,运行“C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe”TestService.exe
  • 运行净启动测试服务
  • 步骤4的输出

    正在运行事务处理安装

    开始安装的安装阶段

    有关详细信息,请参阅日志文件的内容 C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestService\TestService\obj\x86\Debug\TestService.exe 大会的进展

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

    正在安装程序集“C:\Users\myusername\Documents\Visual Studio” 2010\Projects\TestS-service\TestService\obj\x86\Debug\TestService.exe'

    受影响的参数包括:

    logtoconsole=

    logfile=C:\Users\myusername\Documents\visualstudio 2010\Projects\TestService\T estService\obj\x86\Debug\TestService.InstallLog

    assemblypath=C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestServ ice\TestService\obj\x86\Debug\TestService.exe

    没有具有RunInstallerAttribute.Yes属性的公共安装程序 可以在C:\Users\myusername\Documents\Visual Studio中找到 2010\Projects\TestService\TestSe-service\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\Projects\Tes t服务\TestService\obj\x86\Debug\TestService.InstallLog

    正在提交程序集“C:\Users\myusername\Documents\Visual Studio” 2010\Projects\TestS-service\TestService\obj\x86\Debug\TestService.exe'

    受影响的参数包括:

    logtoconsole=

    logfile=C:\Users\myusername\Documents\visualstudio 2010\Projects\TestService\T estService\obj\x86\Debug\TestService.InstallLog

    assemblypath=C:\Users\myusername\Documents\Visual Studio 2010\Projects\TestServ ice\TestService\obj\x86\Debug\TestService.exe

    没有具有RunInstallerAttribute.Yes属性的公共安装程序 可以在C:\Users\myusername\Documents\Visual Studio中找到 2010\Projects\TestService\TestSe-service\obj\x86\Debug\TestService.exe 大会

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

    提交阶段已成功完成

    已完成事务处理的安装

    步骤5的输出

    服务名称无效

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

    查看:

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

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


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

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

    它不会直接安装在盒子里。。。您需要首先创建安装程序类

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

    很老了。。。但这就是我所说的:

    通过执行此操作,将自动创建
    ProjectInstaller.cs
    。然后,您可以双击该按钮,进入设计器并配置组件:

    • serviceInstaller1
      具有服务本身的属性:
      Description
      DisplayName
      ServiceName
      StartType
      是最重要的

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

    例如:

    this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
    
    两个典型问题:

  • 缺少ProjectInstaller类(正如@MiguelAngelo所指出的)
  • 命令提示符必须“以管理员身份运行”
  • 另一个可能的问题(我遇到):

    确保
    ProjectInstaller
    类是
    public
    。老实说,我不确定我到底是如何做到的,但我将事件处理程序添加到了
    ProjectInstaller.Designer.cs
    ,如:

    this.serviceProcessInstaller1.BeforeInstall+=new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1\u BeforeInstall)

    我猜在
    ProjectInstaller.cs
    中自动创建处理程序函数的过程中,它更改了

    public类ProjectInstaller:System.Configuration.Install.Installer

    分部类ProjectInstaller:System.Configuration.Install.Installer

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

    public分部类ProjectInstaller:System.Configuration.Install.Installerusing 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);
            }
        }
    }
    
    RunInstaller(true)   // old alias  
    
    RunInstallerAttribute(true)  // new property spelling