2

I have a console application that will optionally self-install itself as a service. This works fine, but I'd like to embed some arguments into the service startup - similar to (for example) Google's Update Service (which has the parameter /medsvc)

enter image description here

So let's say I'd like my service to start

MyService.exe RUN Test1

.. so that'd start up MyService.exe with the parameters RUN and Test1.

I can install the service fine, using

  ManagedInstallerClass.InstallHelper(new[] {Assembly.GetExecutingAssembly().Location});

However, there's no parameters on the service. So if I try:

  ManagedInstallerClass.InstallHelper(new[] {Assembly.GetExecutingAssembly().Location +" RUN Test1"});

I get a FileNotFoundException. Giving that it's a array, I thought I'd try:

ManagedInstallerClass.InstallHelper(new[] {Assembly.GetExecutingAssembly().Location,"RUN","Test1"});

.. which gives the same exception, except that it's trying to find the file RUN now.

I can't find any specific documentation on how to achieve this - does anyone know if it is possible to embed parameters in with the service executable path? As another example, here's Google's Update Service with parameters - I'd like to ultimately achieve the same.

KenD
  • 5,280
  • 7
  • 48
  • 85

3 Answers3

1

It took me a while to find this out, I hope it's still useful to someone. First I found out, that you are not supposed to run ManagedInstallerClass.InstallHelper according to MSDN docs:

This API supports the product infrastructure and is not intended to be used directly from your code.

Then I found out I could just use my own ProjectInstaller (a component class I added containing a Service Installer and a Service Process Installer) to install the service like this:

ProjectInstaller projectInstaller = new ProjectInstaller();
            string[] cmdline = { string.Format("/assemblypath={0} \"/myParam\"", Assembly.GetExecutingAssembly().Location) };
            projectInstaller.Context = new InstallContext(null, cmdline);

            System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
            projectInstaller.Install(state);

Be sure to encapsulate your parameters in quotes and escape the quotes, otherwise your parameters will become part of the executable path and fail to start.

The end result will be a new service with the specified properties in your Service Installer and Service Process Installer, and a path just like in your screenshot (with the /medsvc parameter for example).

John Smith
  • 965
  • 1
  • 9
  • 22
0

I use a console application inside my windows service. The Main method in Program.cs processes the command line args. The OnStart method starts the console application. It works great.

Windows Service to Run Constantly

HybridService Easily Switch Between Console Application and Service

clairestreb
  • 1,243
  • 12
  • 24
-1

Only parameters before location are being passed into the context for the installer. Try this:

args = new[] { "/ServiceName=WinService1", Assembly.GetExecutingAssembly().Location };
ManagedInstallerClass.InstallHelper(args);

Reference from another answer: Passing Parameter Collection to Service via InstallHelper

Community
  • 1
  • 1
D_Learning
  • 1,033
  • 8
  • 18
  • 1
    I'm afraid that doesn't appear to do anything - although the service installs with that code, it still doesn't have any parameters on the executable path. – KenD Jul 11 '16 at 17:42