views:

75

answers:

3

Hey stackoverflow,

I have written a pretty simple Windows service in C# that starts up automatically and runs A-okay. I was wondering the best method to have the service accept command-line input -- it will always be from the same user (admin), and the service itself is fully trusted (LocalSystem).

I know a little (very little) about design pattern terminology -- should I be looking to wrap this simple service in an Interpreter-style pattern? Is this too complex, and is there a way to do it through the program's entry point? (As if I was just doing a simple Console app that could take string[] args?)

Basically it should accept some very specific grammar (specific commands) which can be issued via console whenever, and only those specific commands (to hopefully avoid any of a variety of security issues).

If this needs clarification, let me know, and thanks for any suggestions (or even solutions)!

+1  A: 

I have done this by having a console application modify my configuration file which my service knew to check for by monitoring the folder for changes.

Raj More
Great idea, but I imagine this would need lots of logic on the service-side to sanity check the parameters in the configuration file. Could be totally wrong. I still might have to go this route, though.
asteroid
Well, I would put the logic in both places - the console app so that it does not mess up the format, and the consuming service, so that it does not blow up while executing.
Raj More
+1  A: 

Instead you can add App.Config file in your project, Windows Service can access app.config on same folder as yourapp.exe.config.

Configuration in .net is highly organized in terms of grammar and customization benefits.

Using App.config will not require you to change your installation settings as well.

Akash Kava
+1  A: 

From what you have described, this is what you are looking for:

Create and communicate with an Unattended Windows Service using .NET

The OnCustomCommand event allows an application to interact with a running service by passing in integer values.

...

Once the service has started you send integer commands to it using the ServiceController's ExecuteCommand method.

Philip Wallace
Really useful, but integer acceptance only will probably not work so well in this case.
asteroid