使用ServiceController类与Windows服务进行通信

时间:2011-12-21 16:17:29

标签: c# .net windows windows-services servicecontroller

我有一个由两个服务service1和service2组成的Windows服务项目。我们如何在两种服务之间进行通信?例如,如何通过service1启动service2?我可以使用ServiceController类来执行此操作吗?你有什么建议这样做的?

2 个答案:

答案 0 :(得分:7)

ServiceController类包含一个带有int的ExecuteCommand方法。因此,如果您可以使用非常有限的参数,则可以通过向对方发送ExecuteCommand消息来进行通信。

然而,正如其他海报评论,WCF可能是一个更好的答案。您可以创建命名管道或在两个服务之间使用HTTP。可以在下面找到两个很好的教程页面。

Basic WCF Tutorial with examples

More complex WCF, if you wanted to 'fake' delegates for example

希望有所帮助。

答案 1 :(得分:6)

如果“comunication”你的意思是开始和停止Service2,那么应该使用ServiceController

如果您需要其他类型的通讯,例如将一些数据从一个发送到另一个,而ServiceController无效,则应使用WCF

希望这有帮助

相关问题