views:

1382

answers:

2

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?

A: 

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
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 B.
Is there a contradiction?
John Saunders
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
Depends. Same command args every time, then configure it. Different args each time, then use Start and pass the arguments.
John Saunders
+1  A: 

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
And here's how, courtesy of Jorg below:http://stackoverflow.com/questions/652654/set-start-parameters-on-service-installation-with-net-serviceinstaller
Steven Sudit
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
Jorg gets the credit, but I'm glad to have directed your attention to his good work.
Steven Sudit