views:

3497

answers:

7

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

+1  A: 

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

hmcclungiii
I don't think so. Considering the user won't be logged in, and will have to specify the folders each time because they may/will change.
Malfist
I see. That would rule out registry entries as well. I agree with ocdecio and Dave though, off the top of my head you should be able to send a string as an argument to a service.
hmcclungiii
+1  A: 

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

ChrisW
Folders change per use. Sorry, I forgot to state that.
Malfist
+1  A: 

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

Otávio Décio
The program will be able to read command line arguments
Malfist
How can I get a service to read command line arguments?
Malfist
When you start the service you can specify them, but only if the service is manually started. For automatic the arguments have to be in the registry (as others pointed out) or in a configuration file.
Otávio Décio
Can I start a service without the user logging in first? I.E. remotely?
Malfist
What about the OnStart(String[] args)?
Malfist
You can control (start and stop) a service remotely (if you're priviledged and have network access): by using an API, by using an O/S GUI, and/or by using the "net start" command from the command line.
ChrisW
You can also change entries in the remote registry, e.g. if the service is reading its startup parameters from the registry.
ChrisW
+1  A: 

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.

Dave Swersky
+1  A: 

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.

Kim Major
A: 

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

Sebastian Sedlak
It's in nice bold lettering in the question. Not a Web Service, therefor WCF is out of the question
Malfist
See my answer for a comment on this.
d7samurai
A: 

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.

d7samurai