简体   繁体   English

Inno Setup for Windows 服务?

[英]Inno Setup for Windows service?

I have a .Net Windows service.我有一个 .Net Windows 服务。 I want to create an installer to install that windows service.我想创建一个安装程序来安装该 Windows 服务。

Basically, it has to do the following:基本上,它必须执行以下操作:

  1. Pack installutil.exe (Is it required?)打包installutil.exe (是否需要?)
  2. Run installutil.exe MyService.exe运行installutil.exe MyService.exe
  3. Start MyService启动我的服务

Also, I want to provide an uninstaller which runs the following command:另外,我想提供一个运行以下命令的卸载程序:

installutil.exe /u MyService.exe

How to do these using Inno Setup?如何使用 Inno Setup 执行这些操作?

You don't need installutil.exe and probably you don't even have rights to redistribute it.您不需要installutil.exe ,可能您甚至没有重新分发它的权利。

Here is the way I'm doing it in my application:这是我在应用程序中执行此操作的方式:

using System;
using System.Collections.Generic;
using System.Configuration.Install; 
using System.IO;
using System.Linq;
using System.Reflection; 
using System.ServiceProcess;
using System.Text;

static void Main(string[] args)
{
    if (System.Environment.UserInteractive)
    {
        string parameter = string.Concat(args);
        switch (parameter)
        {
            case "--install":
                ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                break;
            case "--uninstall":
                ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                break;
        }
    }
    else
    {
        ServiceBase.Run(new WindowsService());
    }
}

Basically you can have your service to install/uninstall on its own by using ManagedInstallerClass as shown in my example.基本上,您可以使用ManagedInstallerClass自行安装/卸载您的服务,如我的示例所示。

Then it's just matter of adding into your InnoSetup script something like this:然后只需将以下内容添加到 InnoSetup 脚本中即可:

[Run]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--install"

[UninstallRun]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--uninstall"

Here's how i did it:这是我如何做到的:

Exec(ExpandConstant('{dotnet40}\InstallUtil.exe'), ServiceLocation, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);

Apparently, Inno setup has the following constants for referencing the .NET folder on your system:显然,Inno setup 有以下常量用于引用系统上的 .NET 文件夹:

  • {dotnet11} {dotnet11}
  • {dotnet20} {dotnet20}
  • {dotnet2032} {dotnet2032}
  • {dotnet2064} {dotnet2064}
  • {dotnet40} {dotnet40}
  • {dotnet4032} {dotnet4032}
  • {dotnet4064} {dotnet4064}

More information available here .此处提供更多信息。

You can use您可以使用

Exec(
    ExpandConstant('{sys}\sc.exe'),
    ExpandConstant('create "MyService" binPath= {app}\MyService.exe start= auto DisplayName= "My Service" obj= LocalSystem'), 
    '', 
    SW_HIDE, 
    ewWaitUntilTerminated, 
    ResultCode
    )

to create a service.创建服务。 See " sc.exe " on how to start, stop, check service status, delete service, etc.有关如何启动、停止、检查服务状态、删除服务等信息,请参见“ sc.exe ”。

If you want to avoid reboots when the user upgrades then you need to stop the service before copying the exe and start again after.如果您想避免在用户升级时重新启动,那么您需要在复制 exe 之前停止服务并在之后重新启动。

There are some script functions to do this at Service - Functions to Start, Stop, Install, Remove a Service有一些脚本函数可以在Service - Functions to Start, Stop, Install, Remove a Service 中执行此操作

have a look at topshelf http://topshelf-project.com/看看topshelf http://topshelf-project.com/

  • it lets you develop your service as a console application它允许您将服务开发为控制台应用程序

  • adds a start/stop service as an API to your service...将启动/停止服务作为 API 添加到您的服务中...

  • ... that you can call from InnoSetup ...您可以从 InnoSetup 调用

    [Run] Filename: "{app}\\myservice.exe"; Parameters: "stop" ; Flags : waituntilterminated Filename: "{app}\\myservice.exe"; Parameters: "uninstall" ; Flags : waituntilterminated Filename: "{app}\\myservice.exe"; Parameters: "install -description ""myservice""" ; Flags : waituntilterminated

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM