Snooks
Snooks

Reputation: 131

Use installutil to install a single service from an executable that has multiple services

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!

Upvotes: 1

Views: 240

Answers (1)

John Koerner
John Koerner

Reputation: 38077

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.

Upvotes: 1

Related Questions