How to Communicate with a Windows Service

Correct way to communicate with a local Windows Service

You can use:

  • Socket communications (TCP, HTTP, or other): even though you say they are network protocols the advantage is that you might already know them
  • Named pipes: this is a good option for communication between processes running in the same node: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365590(v=vs.85).aspx
  • shared memory: this is the fastest
  • Other third party, higher level, library like Thrift which uses sockets

Communicate with clients from windows service

Original project was declined and only recently it was brought up again. I've revisited the problem and ended up with two solutions:

  1. use Managed Extensibility Framework to load 3rd party pluggable modules into local system context (via interface). This can be used when plugin required local system access permissions.

  2. use WCF for inter-process communication (say, using named pipes transport for local clients) when 3rd party module do not require local system access permissions and can run under different security context.

A windows service to communicate with server application

You can use WCF with named pipes, it's clear and very easy to use.
Check this to more information about named pipes in WCF.

You should also try to know a little more about WCF before you begin the coding.´

Good luck.

PS: Don't get scarred with "what you heard", I don't really think WCF is so advanced level as you may heard, you only need to understand the concepts that are bound with WCF, the syntax and maintenance of the code is very easy to understand after a few examples, give WCF a chance and it will make you're live so much easier.

How to communicate with a windows service from an application that interacts with the desktop?

Be aware that if you are planning to eventually deploy on Windows Vista or Windows Server 2008, many ways that this can be done today will not work. This is because of the introduction of a new security feature called "Session 0 Isolation".

Most windows services have been moved to run in Session 0 now in order to properly isolate them from the rest of the system. An extension of this is that the first user to login to the system no longer is placed in Session #0, they are placed in Session 1. And hence, the isolation will break code that does certain types of communication between services and desktop applications.

The best way to write code today that will work on Vista and Server 2008 going forward when doing communication between services and applications is to use a proper cross-process API like RPC, Named Pipes, etc. Do not use SendMessage/PostMessage as that will fail under Session 0 Isolation.

http://www.microsoft.com/whdc/system/vista/services.mspx

Now, given your requirements, you are going to be in a bit of a pickle. For the cross-platform concerns, I'm not sure if Remoting would be supported. You may have to drop down and go all the way back to sockets: http://msdn.microsoft.com/en-us/library/system.net.sockets.aspx

How to communicate to Windows Service through WCF?

You can create a communication channel between your MyService (WCF Hosting entity) and the MyCommunicationService (WCF Service instance) simply using a static property:

//This can be put into a separate DLL if needed.
public interface IMyHostCallbacks
{
void HostCallback(string iSomeDataFromWCFToHost);
}

public static class Host
{
public static IMyHostCallbacks Current;
}

public partial class MyService : ServiceBase, IMyHostCallbacks
{
protected override void OnStart(string[] args)
{
//Set the static callback reference.
Host.Current = this;

if (m_svcHost != null) m_svcHost.Close();

m_svcHost = new ServiceHost(typeof(MyCommunicationService));
m_svcHost.Open();

// initialize and work with myObject
}

//Here you have data from WCF and myObject available.
void IMyHostCallbacks.HostCallback(string iSomeDataFromWCFToHost)
{
//Be careful here.
//Depending on your WCF service and WCF clients this might
//get called simultaneously from different threads.
lock(myObject)
{
myObject.DoSomething(iSomeDataFromWCFToHost);
}
}
}

Surely you can even put the static field into your MyService class but at least some abstraction between MyService and MyCommunicationService would not hurt.


Now, in any place in your MyCommunicationService you can:

void WCFServiceMethod()
{
Host.Current.HostCallback("some data");
}

Communication between Windows Service and Desktop Application

Don't get scared away by the named pipes concept, WCF will take care of the nasty bits for you. You'll only have to do the configuration, it's simply another communication channel, instead of using HTTP for example. So you'll need to look into WCF.

Check out this tutorial.



Related Topics



Leave a reply



Submit