Alex Curtis
Alex Curtis

Reputation: 5767

Wix ServiceInstall Arguments

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" />

Upvotes: 7

Views: 6917

Answers (3)

riskeez
riskeez

Reputation: 103

Haven't really found an answer here, so that is how I solved the same problem:

There are two sets of parameters that are available for Windows Services:

1) When you use <ServiceInstall .. Arguments="YOUR Service Args"> element, your arguments will be added to "Path to executable" ("ImagePath" in registry) of the service.

Use Environment.GetCommandLineArgs method to get the arguments (be aware that first parameter returned by the method is a service name).

Remember to save your arguments to configuration file or registry and read them before installation! Because if you run repair/change installation function, these arguments will be gone.

2) Start parameters (one-time parameters).

These parameters are parameters that are available in OnStart(string[] args) method when you run the service.

Upvotes: 1

user2283288
user2283288

Reputation: 96

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>

Update: The WIX documentation is tragically unaspiring when it comes to this element.

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 [email protected]", 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).

Upvotes: 7

doveryai
doveryai

Reputation: 61

anybody make progress on this? I don't see this arguments hitting my service at startup:

          <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"/>

my service always gets an empty arg array:

    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();
    }

Upvotes: 2

Related Questions