首页 文章

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

提问于
浏览
120

当我在Visual Studio 2010中创建新的Windows服务时,我收到消息,说明使用InstallUtil和net start来运行该服务 .

我尝试了以下步骤:

  • 创建新项目文件 - >新建 - >项目 - > Windows服务

  • 项目名称:TestService

  • 按原样构建项目(Service1构造函数,OnStart,OnStop)

  • 打开命令提示符,运行 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" TestService.exe

  • 运行 net start TestService .

Output of step 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 tService \ TestService \ _dj \ x86 \ Debug \ TestService.InstallLog . 安装程序集'C:\ Users \ myusername \ Documents \ Visual Studio 2010 \ Projects \ TestS ervice \ TestService \ _dj \ x86 \ Debug \ TestService.exe' . 受影响的参数是:logtoconsole = logfile = C:\ Users \ myusername \ Documents \ Visual Studio 2010 \ Projects \ TestService \ T \ testService \ 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中找到rvice \ 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 tService \ TestService \ _dj \ x86 \ Debug \ TestService.InstallLog . 提交程序集'C:\ Users \ myusername \ Documents \ Visual Studio 2010 \ Projects \ TestS ervice \ TestService \ _obj \ x86 \ Debug \ TestService.exe' . 受影响的参数是:logtoconsole = logfile = C:\ Users \ myusername \ Documents \ Visual Studio 2010 \ Projects \ TestService \ T \ testService \ 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中找到rvice \ obj \ x86 \ Debug \ TestService.exe程序集 . 删除InstallState文件,因为没有安装程序 . 提交阶段成功完成 . 事务安装已完成 .

Output of step 5

服务名称无效 .

输入NET HELPMSG 2185即可获得更多帮助 .

6 回答

  • 11

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

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

    Some reference on service installer:

    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;
    
  • 1

    看着:

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

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

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

  • 219

    这是制作安装程序并摆脱该错误消息的另一种方法 . 此外,似乎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);
            }
        }
    }
    
  • 8

    两个典型问题:

    • 缺少ProjectInstaller类(正如@MiguelAngelo指出的那样)

    • command prompt 必须“以 Administrator 运行”

  • 3

    另一个可能的问题(我遇到过):

    确保 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

    replacing publicpartial 的关键字 . 所以,为了解决它必须

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

    我使用Visual Studio 2013社区版 .

  • 4

    Stealth Change in VS 2010 and .NET 4.0 and Later

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

    在.NET中有一个别名更改或编译器清理,可能会针对您的具体情况揭示这一小调整 .

    如果您有以下代码......

    RunInstaller(true)   // old alias
    

    您可能需要将其更新为

    RunInstallerAttribute(true)  // new property spelling
    

    它就像在编译时或运行时在封面下更改了别名,您将获得此错误行为 . 上面对RunInstallerAttribute(true)的显式更改将其修复在所有计算机上的所有安装方案中 .

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

相关问题