开发者

How to control of windows service from a another desktop apps

suppose i have a windows service which does some work and developed by c#. now i want to develop a another win apps with c# which control & interact with my win service. suppose i want to start/stop my win service from my win apps or i want to run a specific method of my win service from my win apps. how to develop this type of 开发者_运维问答win apps which can interact with my win service. please discuss in detail.

thanks


You may find the following article useful.


For starting and stopping see ServiceController class

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.aspx

You can also pass parameters on start.


For controlling use the ServiceController class as stated by the other.

Do make your service to do something special controlled by your app you have to set up a kind of communication. I would suggest your service listens on a certain port for http request which can be handled fairly easy and so your service might be controlled by any kind of app even javascript ajax. If unwanted add some security like authorization.


For communication between a Windows service and a Windows application, I use Windows Communication Foundation (WCF). In my Windows service, I host a WCF service that "listens" for incoming requests. My Windows application creates a WCF client that communicates with the WCF service hosted in the Windows service. From a programmatic point of view, the WCF client invokes methods on the WCF service. All of the behind-the-scenes complexity is hidden, so from a programmer's perspective, it looks like you are simply making a function call.

To start and stop the service, I use the ServiceController class. Note that this class has certain security requirements. A user with limited system privileges will be unable to use this class directly.


You need to be aware of security.

  • Using the ServiceController class gives you some limited ability to control a service (start/stop/pause/resume and send simple commands), and has the advantage of being secure. Typically only Administrators (and maybe Power Users) will be able to control the service although you can configure permissions.

  • If you need something more flexible, you need some form of inter-process communication to communicate with the service - and WCF is a good option in the .NET world as described in Matt Davis' answer.

    However in this case you need to implement your own security, assuming you don't want to allow just anyone to interact with your service.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜