3

How do I change settings of Windows Service while it's running?

Let's say I process files from one directory to another. I'd like to be able to declare the output and operations for next files (while the service is running). Would be perfect if I could edit it remotely via website (SQL tables or maybe XML/TXT files).

Is it at all possible without restarting service? Could I simply write inside service "for every file read operations and output from settings file"?

I'd rather avoid for now Windows Communication Foundation.

yosh
  • 3,245
  • 7
  • 55
  • 84

5 Answers5

3

Assuming you also wrote the Windows Service, you could send a custom command to the service. From the controller side, you use ServiceController.ExecuteCommand() to send the command. On the service class, you override ServiceBase.OnCustomCommand() to process the commands.

This will only help you trigger an action, however. You will still have to use an external method (e.g. shared configuration file) to convey the details of the action.

seairth
  • 1,966
  • 15
  • 22
2

yosh - the two options you proposed would work, the service could read a file settings.xml repeatedly before it processes a folder. You could tinker with that remotely without restarting the service.

phoenixAZ
  • 429
  • 3
  • 17
2

You could set up a FileSystemWatcher to monitor a configuration file, so that config changes are reloaded as soon as they are updated.

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

DaveRead
  • 3,371
  • 1
  • 21
  • 24
1

Many options. There are many interprocess communication mechanisms you can employ - COM, sockets, named pipes, shared memory - to provide an administrative interface with a "re-read your settings" command.

Alternatively, you can have the service watch for config file changes; in Win32 API, the relevant function is FindFirstChangeNotification(); not sure if there's a managed equivalent, worst case - use P/Invoke.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
0

You can use a database and save and retrieve the information, but unless you're already using a database this may be overkill to set up a new table, connect to it, etc.

I personally would recommend playing with the *.config and using the OpenExeConfiguration. Also, expose a couple of methods that can access/modify those settings then use them whenever you need the value.

Lastly you could have your own XML file that has those values and store/retrieve the values (and even take advantage of the XmlSerializer) which can be queried when it comes time to process.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200