简体   繁体   English

与运行Windows服务交互?

[英]Interacting with running Windows Service?

How do I change settings of Windows Service while it's running? 如何在Windows服务运行时更改其设置?

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). 如果我可以通过网站(SQL表格或XML / TXT文件)远程编辑它将是完美的。

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. 我宁愿现在避免使用Windows Communication Foundation。

Assuming you also wrote the Windows Service, you could send a custom command to the service. 假设您还编写了Windows服务,则可以向服务发送自定义命令。 From the controller side, you use ServiceController.ExecuteCommand() to send the command. 从控制器端,使用ServiceController.ExecuteCommand()发送命令。 On the service class, you override ServiceBase.OnCustomCommand() to process the commands. 在服务类上,重写ServiceBase.OnCustomCommand()以处理命令。

This will only help you trigger an action, however. 但是,这只会帮助您触发操作。 You will still have to use an external method (eg shared configuration file) to convey the details of the action. 您仍然必须使用外部方法(例如共享配置文件)来传达操作的详细信息。

yosh - the two options you proposed would work, the service could read a file settings.xml repeatedly before it processes a folder. yosh - 您提议的两个选项可以使用,服务可以在处理文件夹之前重复读取文件settings.xml。 You could tinker with that remotely without restarting the service. 您无需重新启动服务即可远程修补。

You could set up a FileSystemWatcher to monitor a configuration file, so that config changes are reloaded as soon as they are updated. 您可以设置FileSystemWatcher来监视配置文件,以便在更新后立即重新加载配置更改。

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

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. 您可以使用许多进程间通信机制 - COM,套接字,命名管道,共享内存 - 以提供具有“重新读取您的设置”命令的管理界面。

Alternatively, you can have the service watch for config file changes; 或者,您可以使用服务监视配置文件更改; in Win32 API, the relevant function is FindFirstChangeNotification() ; 在Win32 API中,相关函数是FindFirstChangeNotification() ; not sure if there's a managed equivalent, worst case - use P/Invoke. 不确定是否存在托管等效,最坏情况 - 使用P / Invoke。

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 . 我个人建议使用* .config并使用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. 最后,您可以拥有自己的XML文件,该文件具有这些值并存储/检索值(甚至可以利用XmlSerializer ),在处理时可以查询这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM