5

I am working on an application which starts as a service but only if a commandline switch tells it to (otherwise a standard form is opened). So when the service is started by Windows at bootup, it must pass this commandline option or the service fails to start.

I would like to have the installer (ServiceProcessInstaller) add a commandline option so that when the service is started it adds the commandline option to the command.

Example: MyService.exe -commandlineoption

I thought this was what the ServiceProcessorInstaller.Context property was for, but that is for the arguments that were executed on InstallUtil.

Any suggestions?

Guss
  • 30,470
  • 17
  • 104
  • 128
Tim
  • 1,621
  • 4
  • 19
  • 35
  • It's not clear from your question: is your application installing the service, starting it, or both? – adrianbanks Jul 09 '09 at 23:27
  • I'm thinking the former. – Steven Sudit Jul 09 '09 at 23:47
  • Sorry for the ambiguity. The application normally is a standard windows forms application. However, in the Main() method, it looks for commandline arguments. If there is a -service commandline option, it starts the service. I have also included an installer clase which InstallUtil looks for in order to register the service with Windows. When the service is started by Windows at bootup, the service should be called with the commandline option. – Tim Jul 10 '09 at 01:14

2 Answers2

1

When I've added command-line options to services, I've always defaulted to running as a service. However, I know that the opposite is possible because it's how SvcHost works: it's an EXE that's always configured to load DLL's as services.

Steven Sudit
  • 19,391
  • 1
  • 51
  • 53
  • And here's how, courtesy of Jorg below: http://stackoverflow.com/questions/652654/set-start-parameters-on-service-installation-with-net-serviceinstaller – Steven Sudit Jul 09 '09 at 23:48
  • The question that you linked to seems to be what I need. I guess I wasn't searching for the right words. They couldn't make it easy, could they?! Thanks Steven! – Tim Jul 10 '09 at 01:27
  • Jorg gets the credit, but I'm glad to have directed your attention to his good work. – Steven Sudit Jul 10 '09 at 02:26
0

A service is only installed once per release. It sounds like you are talking about passing a command line argument to the service when it's started.

You can pass command line arguments to the service when you start it using the ServiceController.Start method:

using (var controller = new ServiceController("servicename")) {
    controller.Start(arg0, arg1);
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • If that's the case see e.g. my Q at http://stackoverflow.com/questions/652654/set-start-parameters-on-service-installation-with-net-serviceinstaller – Jörg Battermann Jul 09 '09 at 23:32
  • John, the question he linked to explains how to configure the service to attach parameters that are passed whenever the service is started by anyone. I think it contains the answer to Tim's question. – Steven Sudit Jul 09 '09 at 23:46
  • Depends. Same command args every time, then configure it. Different args each time, then use Start and pass the arguments. – John Saunders Jul 10 '09 at 05:47