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

jkh*_*jkh 134 c# windows service windows-services installutil

当我在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\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\_dj\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\_dj\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文件,因为没有安装程序.

提交阶段成功完成.

事务处理安装已完成.

步骤5的输出

服务名称无效.

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

Mig*_*elo 236

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

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

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

如何:向服务应用程序添加安装程序

很老......但这就是我所说的:

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

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

  • serviceInstaller1有服务本身的属性:Description,DisplayName,ServiceNameStartType是最重要的.

  • serviceProcessInstaller1有这个重要的属性:Account这是服务将运行的帐户.

例如:

this.serviceProcessInstaller1.Account = ServiceAccount.LocalSystem;
Run Code Online (Sandbox Code Playgroud)

  • 大声笑.我喜欢"Quite old"链接指向一个名为Arcane Code的网站.该页面越旧,名称越真实:-) (4认同)
  • 添加安装程序并将帐户设置为LocalSystem就可以了.谢谢! (3认同)

Jam*_*are 11

看着:

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

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

Ps我有自己的小自助安装/可调试Windows服务模板,你可以从中复制代码或使用:Debuggable,自安装Windows服务


Car*_*rol 8

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


fab*_*tto 5

两个典型问题:

  1. 缺少ProjectInstaller类(如@MiguelAngelo所指出的)
  2. 命令提示符下必须“运行方式管理员