Jim
Jim

Reputation: 2146

Client/Server (Service) Communications for a c# Windows Service

I have a Windows Service that I am in the process of writing. I don't have a problem with the service in and of itself, but this is the first service that I need to have some ad-hoc communications with a client (others have just been statically configured via the .config file). I need to have a client talk to the service and send it messages and get back replies.

At first flush I thought of WCF, but I can't seem to resolve how to get my Windows Service and WCF to talk to each other. I can have the Windows Service host the WCF service, but that is not the same as getting an instance so I could say, wire up an event. I also don't see a way of getting the WCF service to get an instance of the running Windows Service so it can talk to the Windows Service.

Either I am missing something, or, I need someone to tell me that I can't "get there from here". If it can't be done I guess I will resort to "low level" Socket calls where the service is handling the communications directly--I was just hoping to avoid doing that.

Thanks in advance,
Jim

EDIT: Sorry I was not clearer on this. I understand how to get the client app to talk to WCF. I am trying to figure out how to get the WCF hosted service talk to the Windows Service or visa-versa. This is in the same binary and I am looking to do this to provide a way for the client to talk to the service (service to service, not client to service).

Upvotes: 1

Views: 2196

Answers (1)

Greg Sansom
Greg Sansom

Reputation: 20810

You can achieve this by hosting a WCF Service from within the Windows Service. The client can then make web-service calls to the WCF Service, which can then make calls into the Windows Service as required.

The below steps assume you want to keep the WCF service in a separate project - this is slightly more difficult than keeping both components in the Windows Service project, but might keep the code a bit more organized.

  1. Start with two projects – WcfService1 and WindowsService1. Create a class named MyServiceWorker which performs the work of the Windows Service.
  2. Add a reference in WindowsService1 to WcfService1.
  3. Define an interface named IMyServiceWorker in WcfService1. This should define the operations on the worker class, and should be implemented by MyServiceWorker in WindowsService1.
  4. In WcfService1.Service1, add “public static IMyServiceWorker myServiceWorker”.
  5. In the WindowsService OnStart() method, create a MyServiceWorker, and pass it to WcfService1.Service1.myServiceWorker.
  6. Open/start MyServiceWorker, then open the WcfService.

Here are the key bits of code for the WcfService:

    public interface IMyServiceWorker
    {
       void ejectCD();
       void volumeUp();
       void volumeDown();
    }

    public class Service1 : IService1
    {
        //this is the reference to your Windows Service.
        public static IMyServiceWorker WindowsServiceWorker;

        public void ejectCD()
        {
            WindowsServiceWorker.ejectCD();
        }

        public void volumeUp()
        {
            WindowsServiceWorker.volumeUp();
        }

        public void volumeDown()
        {
            WindowsServiceWorker.volumeDown();
        }
    }

And here is the code for the WindowsService:

  public class MyServiceWorker:IMyServiceWorker
    {
        //You will need to implement these methods.

        public void ejectCD()
        {
            throw new NotImplementedException();
        }

        public void volumeUp()
        {
            throw new NotImplementedException();
        }

        public void volumeDown()
        {
            throw new NotImplementedException();
        }

        internal void Open()
        {
            throw new NotImplementedException();
        }
    }

Upvotes: 3

Related Questions