Malfist
Malfist

Reputation: 31805

Passing a Windows Service Parameters for it to act on

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

Upvotes: 2

Views: 19963

Answers (10)

David Wazy
David Wazy

Reputation: 11

RE: config file.

Of course a config file can be used. And the file can be changed while the service is running.

This would be a nice solution if the config file changes in fact. All my services use an XML config file, wrapped in a class for easy reuse.

The wrapper has an option to monitor the XML file using fileMonitor for changes, optionally refreshing the content of the config file automatically, and finally raises an event to the service class. The service then has the option of "resetting" itself as needed to incorporate the new values in the XML configuration file.

Placing configuration into the registry has a few issues:

  • Security (ie: installer being granted access), depending on what tree is used
  • The service will not be aware of changes
  • Portability - although minor as the install should setup registry settings

Where an XML file is easy to copy, edit, share, view and understand. Throw in some good COMMENT blocks and a detailed XSD file, and it becomes a source of good documentation too.

Look into XPath for easy navigation and extraction of values within the XML file.

$0.02 ... david ...

Upvotes: 1

Random Dev
Random Dev

Reputation: 52290

Concerning the app.config file - I'm rather sure that your service will read and use those files as I write all my windows-services this way ;) So just put everything you need in the app.config under "application" (not user) and put allway edit the "yourname.exe.config" in the folder where you "InstallUtil" the service from.

Upvotes: 0

Karl
Karl

Reputation: 21

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 #

Upvotes: 2

d7samurai
d7samurai

Reputation: 3216

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.

Upvotes: 2

Sebastian
Sebastian

Reputation: 819

Why not just Host a WCF Service in the Windows Service to obatain such "admin" functions? (Remoting is also possible)

Upvotes: 1

Kim Major
Kim Major

Reputation: 3711

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.

Upvotes: 6

Dave Swersky
Dave Swersky

Reputation: 34810

Windows services have executables like any other. I believe you can write it to accept command-line parameters and specify those parameters in the Windows Service configuration. You can also have it read a config file. If you're using .NET, there are config file classes in the framework.

Upvotes: 1

Otávio Décio
Otávio Décio

Reputation: 74290

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

Upvotes: 1

ChrisW
ChrisW

Reputation: 56123

Store the service's startup parameters in the registry: and then, when the registry starts, it should read its startup parameters from the registry.

Upvotes: 1

hmcclungiii
hmcclungiii

Reputation: 1805

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

Upvotes: 1

Related Questions