1

I have created an executable that has 2 services defined, by following steps similar to the post at: Can I have multiple services hosted in a single windows executable

In this case, if I use installutil.exe to install the service, it looks like it installs all the services defined (2 in this case). With this implementation, is there a way to have installutil.exe only install the service I specify in a command line (e.g. installutil.exe /service=Service1 ), instead of all of the services defined?

Thanks!

Community
  • 1
  • 1
Snooks
  • 91
  • 1
  • 11

1 Answers1

1

Yes. You can access the command line from the Context of the project installer and only run the installers that you want.

For example, if I override the install on the project installer, I can then check the command line to see what to do.

public override void Install(IDictionary stateSaver)
{
    var foo = Context.Parameters["foo"];
    Console.WriteLine($"Foo is {foo}");
    if (foo.Equals("bar"))
    {
        Console.WriteLine("Installing Service1");
        this.Installers.Remove(serviceInstaller2);
        base.Install(stateSaver);
    }
    else if (foo.Equals("baz"))
    {
        Console.WriteLine("Installing Service2");
        this.Installers.Remove(serviceInstaller1);
        base.Install(stateSaver);
    }
}

I then call the installutil exe like this:

installutil /foo="bar" WindowsService1.exe

It is important to note that your command line parameters need to come before the assembly that contains your service installers.

John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • that works beautifully, thank you! The only additional thing I had to do was to make sure I also overrode the "Uninstall" method with essentially the same code, and that helped both selectively install and uninstall the services. – Snooks Jan 23 '17 at 19:11
  • Actually, @John there seems to be another problem. Let me know if this should be a separate question. If I do this approach, and only install one of the services on a given server, the code that executes when I start the service is always the code from the first service in the entry point, and not the other, irrespective of what service I install. Haven't been able to figure out why that is. Is there something else that I need to do to make sure the code for the correct service runs? – Snooks Feb 22 '17 at 20:13