【问题标题】:Wix ServiceInstall ArgumentsWix 服务安装参数
【发布时间】:2012-05-24 03:58:46
【问题描述】:

有谁知道如何让我在 ServiceInstall 中声明的参数在服务启动时传递给服务?它们在我的 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" />

【问题讨论】:

  • 您或许应该考虑改用Windows Service Config File
  • 是的。我可以那样做。我只是想看看为什么这些论点不起作用。
  • 但是他们在哪里不工作?安装程序没有正确处理它们吗?正如所写,听起来您将安装程序排除在应用程序配置之外。
  • 在启动时,这些参数似乎从未影响我的服务。除非(我可能)误解了文档。当从安装程序启动时,这些参数应该被触发到服务中。

标签: c# wix


【解决方案1】:

有点老了,但这是你可以做的

          <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 文档在涉及this 元素时令人悲哀地令人失望。

基本上,您可以设置(公共)WIX 变量,通常定义为 [P_*](例如 msiexec 参数、静态或在 CA 中)。这些值在启动时以相同的方式传递给服务,就像您在从服务控制台启动服务时(或者我想象的使用 net start)启动服务时将这些值连接到一个字符串中一样。就我而言,这些是空格分隔的值,例如[P_USERMAIL] 是“--useremail some@email.com”,尽管这是任意的,因为您将在您发布的服务启动代码中处理它。

您可能知道,这些值不会持久化。如果服务无法使用您提供的值进行初始化,您将需要重新安装/修复或以其他方式将它们传递给服务(即服务控制台、网络启动)。

【讨论】:

  • 您能解释一下您发布的内容吗?
【解决方案2】:

有人在这方面取得进展吗?我没有看到这个参数在启动时影响我的服务:

          <ServiceInstall
            Id="ServiceInstaller"
            Type="ownProcess"
            Vital="yes"
            Name="Service"
            DisplayName="Service"
            Description="a service" 
            Arguments="-p stuff"
            Start="auto"
            Account="LocalSystem"
            ErrorControl="normal"
            Interactive="yes"/>

我的服务总是得到一个空的 arg 数组:

    partial class PrintMonitorService : ServiceBase
    {
    private readonly PrintMonitorServiceManager _serviceManager;

    public PrintMonitorService()
    {
        InitializeComponent();
        _serviceManager = new PrintMonitorServiceManager();
    }

    protected override void OnStart(string[] args)
    {
        _serviceManager.StartHosting(args);
    }

    protected override void OnStop()
    {
        _serviceManager.StopHosting();
    }

【讨论】:

  • 这里也一样。你找到任何解决方案了吗? :(
  • 在启动时(在 main() 中)传递给 exe 的参数和在服务启动时传递的参数是有区别的。这可能会让你感到困惑。 main() 获取 argc 和 argv,servicebase.cpp 在启动时通过以下方式获取自己的参数: virtual void OnStart(DWORD dwArgc, LPWSTR* pszArgv);
【解决方案3】:

这里还没有真正找到答案,所以我就是这样解决了同样的问题:

two sets of parameters that are available for Windows Services:

1) 当您使用&lt;ServiceInstall .. Arguments="YOUR Service Args"&gt; 元素时,您的参数将被添加到服务的“可执行文件路径”(注册表中的“ImagePath”)中。

使用Environment.GetCommandLineArgs 方法获取参数(注意该方法返回的第一个参数是服务名称)。

记得把你的参数保存到配置文件或注册表中,安装前阅读它们!因为如果你运行修复/更改安装功能,这些参数就会消失。

2) 启动参数(一次性参数)。

这些参数是您在运行服务时在 OnStart(string[] args) 方法中可用的参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 2020-09-05
    相关资源
    最近更新 更多