Passing a Windows Service Parameters for it to act on

18,992

Solution 1

Any service is capable of receiving command line arguments at start-up.

Solution 2

You can instantiate your service and pass command line arguments using the ServiceController class.

using (ServiceController serviceController = new ServiceController(serviceName))
{
   string[] args = new string[1];
   args[0] = "arg1";
   serviceController.Start(args);
}

"arg1" will then be available as regular command line arguments in main() when Windows starts up the service.

Solution 3

I see that you (or someone) voted Sebastian Sedlak's answer down, because he mentioned hosting a WCF Service in the Windows Service. Your reply was

It's in nice bold lettering in the question. Not a Web Service, therefor WCF is out of the question

I think you misunderstood what he meant. He wasn't talking about a Web Service. He was talking about hosting a WCF Service within your Windows Service.

It's far from the same thing. You can host a WCF Service within any Windows (Forms/Console/Service) application. The point of doing so, is that the application is then reachable for communciation via its internal WCF Service, in the same fashion as you can communicate with a Web Service (you can also host WCF Services in IIS, btw, which would then make them "Web Services", in the sense you seem to be referring to).

In a Windows Service, this means you can send any command to it and also get any information you want from it - while it's running.

In fact, I am working on a project right now, which is a Windows Service that I need to be able to contact and pass commands to - and get information from - at runtime. For example, I want to be able to tell it where to store certain things, what to log, to have it reset/restart - and poll it for status messages. I do this by hosting a WCF Service inside the Windows Service. That WCF Service exposes a set of methods, that in my case includes receiving commands and returning status information. So when the Windows Service is running, I can contact it (even remotely), via its built-in WCF Service and tell it what to do.

This an extremely easy thing to implement, and in the case of Windows Services, can provide you with a much richer interface to the Service than through the basic standard commands.


However, you specified that you wanted your Windows Service to receive its folder settoings each time it starts up, which makes such a passive setup less than ideal (as it would be unable to do anything until you passed it the right folders).

One way to deal with this (using a hosted WCF Service), would be to have the Windows Service running all the time (i.e. automatic startup). Its default state would be idle. Then you could issue it a "start processing"-command, feeding it the correct folders to work on (through a call to the corresponding WCF Service method). Similarly, the WCF Service would expose methods giving you the status of the application (current folder, progress, busy/idle etc). Once the processing is done, it would go back into the idle state, waiting for the next set of folders to be supplied to it.

Doing it this way would make it very easy to control remotely - you could even make an online administration panel for it, accessible from anywhere.

Solution 4

The issue, is that, while passing in parameters is not difficult, when the machine restarts and windows tries to restart the service, those parameters are not there. they only exist when someone starts the service from the command line.

for example. I have a windows service which hosts a WCF service. I want the users to be able to specify a non-default port number for the WCF service to listen on. They do this by starting the windows service like so... MyService -port:xxxxx

Which works fine, until the server is rebooted, then windows restarts MyService (but without parameters) and the wcf service defaults to original port #

Solution 5

Would it be possible to use a configuration file to specify these items?

Share:
18,992
Programatt
Author by

Programatt

I am a programmer by day, and a tinkerer by night.

Updated on June 05, 2022

Comments

  • Programatt
    Programatt almost 2 years

    I want to turn a program I have into a service so I can use it without logging it. Basically what it does is it backs up specified folders to a specified location using SSH. However the problem I'm running into is I don't know how to tell it these items. I only know how to start, stop, and run a custom command with only an integer with a parameter.

    How can I do this?

    Windows Service, not a Web Service

    edit: The folders it backs up will not remain consistent and will be updated at every runtime