问题描述
有谁知道如何让我在 ServiceInstall 中声明的参数在服务启动时传递给服务?它们在我的 OnStart(string[] args) 中似乎总是为空.
Does anyone know how to get the arguments I declare in ServiceInstall to be passed to the service when it starts up? They always seem to be null in my OnStart(string[] args).
<ServiceInstall
Id="ServiceInstaller"
Type="ownProcess"
Vital="yes"
Name="MyService"
DisplayName="MyService"
Description="MyService Desc"
Start="auto"
Account="LocalSystem"
ErrorControl="ignore"
Interactive="yes"
Arguments="MY ARGS HERE"
>
</ServiceInstall>
<ServiceControl Id="ServiceControl" Start="install" Stop="both" Remove="uninstall" Name="MyService" Wait="yes" />
推荐答案
有点老,但你可以这样做
Kind of old, but here's what you can do
<ServiceInstall
Id="SomeService"
Type="ownProcess"
Vital="yes"
Name="Some Service"
DisplayName="Some Service"
Description="Monitoring and management of some service"
Start="auto"
Account="LocalSystem"
ErrorControl="normal"
Interactive="no"/>
<ServiceControl Id="StartSomeService" Start="install" Stop="both" Remove="uninstall" Name="Some Service" Wait="yes">
<ServiceArgument>[P_USEREMAIL]</ServiceArgument>
<ServiceArgument>[P_USERPASSWORD]</ServiceArgument>
<ServiceArgument>[P_DEFAULTNAMINGCONTEXT]</ServiceArgument>
<ServiceArgument>[P_USEHTTPPROXY]</ServiceArgument>
<ServiceArgument>[P_PROXYADDRESS]</ServiceArgument>
<ServiceArgument>[P_PROXYDOMAIN]</ServiceArgument>
<ServiceArgument>[P_PROXYUSERNAME]</ServiceArgument>
<ServiceArgument>[P_PROXYPASSWORD]</ServiceArgument>
</ServiceControl>
更新:WIX 文档对于这个 元素.
Update: The WIX documentation is tragically unaspiring when it comes to this element.
基本上,您可以设置(公共)WIX 变量,通常定义为 [P_*](例如 msiexec 参数、静态或在 CA 中).这些值在启动时以相同的方式传递给服务,就像将这些值连接到一个字符串中一样,当从服务控制台(或我想象的 net start)启动服务时,您将这些值作为启动参数提供.就我而言,这些是空格分隔的值,例如[P_USERMAIL] 是--useremail some@email.com",尽管这是任意的,因为您将在您发布的服务启动代码中处理它.
Basically, you can set the (public) WIX variables, defined as [P_*] per usual (e.g. msiexec arguments, static, or in a CA). These values are passed to the service at startup in the same manner as if you concatenated these values in a string that you supply as start parameters when starting a service from the services console (or with net start I imagine). In my case, these were space delimited values, e.g. [P_USERMAIL] is "--useremail some@email.com", although this is arbitrary as you'll handle this in the service start code that you posted.
您可能知道,这些值不会持久化.如果服务无法使用您提供的值进行初始化,您将需要重新安装/修复或以其他方式将它们传递给服务(即服务控制台、网络启动).
As you probably know, these values are not persisted. If the service fails to initialize with the values as you provided, you will need to either re-install/repair or pass them in to the service another way (i.e. services console, net start).
这篇关于Wix 服务安装参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!