我有一个 .Net Windows 服务。我想创建一个安装程序来安装该 Windows 服务。
基本上,它必须执行以下操作:
- 包
installutil.exe
(需要吗?) - 运行
installutil.exe
我的服务.exe - 启动我的服务
另外,我想提供一个运行以下命令的卸载程序:
installutil.exe /u MyService.exe
如何使用 Inno Setup 进行这些操作?
我有一个 .Net Windows 服务。我想创建一个安装程序来安装该 Windows 服务。
基本上,它必须执行以下操作:
installutil.exe
(需要吗?)installutil.exe
我的服务.exe另外,我想提供一个运行以下命令的卸载程序:
installutil.exe /u MyService.exe
如何使用 Inno Setup 进行这些操作?
你不需要installutil.exe
,可能你甚至没有权利重新分发它。
这是我在我的应用程序中这样做的方式:
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());
}
}
ManagedInstallerClass
基本上,您可以使用我的示例中所示的方式让您的服务自行安装/卸载。
然后只需在您的 InnoSetup 脚本中添加如下内容:
[Run]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--install"
[UninstallRun]
Filename: "{app}\MYSERVICE.EXE"; Parameters: "--uninstall"
我是这样做的:
Exec(ExpandConstant('{dotnet40}\InstallUtil.exe'), ServiceLocation, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
显然,Inno setup 有以下常量用于引用系统上的 .NET 文件夹:
更多信息可在此处获得。
您可以使用
Exec(
ExpandConstant('{sys}\sc.exe'),
ExpandConstant('create "MyService" binPath= {app}\MyService.exe start= auto DisplayName= "My Service" obj= LocalSystem'),
'',
SW_HIDE,
ewWaitUntilTerminated,
ResultCode
)
创建服务。有关如何启动、停止、检查服务状态、删除服务等,请参见“ sc.exe ”。
如果您想在用户升级时避免重新启动,那么您需要在复制 exe 之前停止服务并在之后重新启动。
服务中有一些脚本函数可以执行此操作- 启动、停止、安装、删除服务的函数
看看topshelf http://topshelf-project.com/
它使您可以将服务开发为控制台应用程序
将启动/停止服务作为 API 添加到您的服务...
...您可以从 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