Call a public method in a Windows service

I have a timer in a windows service (.NET C #). I need to be able to change the past value from an external program. If the timer is currently running, I need to be able to reduce the time elapsed from the external program. I thought that there will be a public method in the service that will change the value of the past timer and restart the timer, but can an external program call the public method in the Windows service?

+6
c # timer windows-services public-method
source share
4 answers

In short, it is not possible to directly call functions in another process. A process containing a function that you want to access (in this case, a Windows service) will have to expose it through some kind of IPC (interprocess communication). What type of IPC you choose will likely depend on how complex the connection is and whether the β€œclient” is a .NET application.

If your needs are simple (for example, just setting a timer value) or if your client does not use .NET, using named pipes (or TCP if you need to access the service from another physical machine) is probably your best bet. Both named pipes and TCP give you Stream, for which you can write messages and read on the other end.

If you need to set up many different functions or send and receive complex data types, and if you use .NET at both ends, then it is probably best to use .NET Remoting or WCF..NET Remoting is simpler, but has more restrictions; WCF is very flexible, but has a steeper learning curve.

+8
source share

Yes it is possible.

You might want to consider creating a NetNamedPipe endpoint in your service and managing the service through this interface.

NetNamedPipeBinding binding = new NetNamedPipeBinding(); MyService myService = new MyService(binding, new EndpointAddress("net.pipe://localhost/MyService")); myService.ResetTimer(30); 
+5
source share

You cannot call a method while Windows is running directly, but you can, for example, include a Windows service in this service as a WCF service. The Windows service will also act as the host of the service. It is possible and not difficult.

+4
source share

Older Services (non WCF) can use .NET Remoting. Look here for information on how to get started. This is the way to WCF for communication between applications across process boundaries.

+2
source share

All Articles