WCF solution needed for a service class

560 views Asked by At

I have a class raises some events and do operations. Eg

class MyService
{
    EventHandler MessageSent;
    EventHandler StatusReceived;

    public void StartService(Serviceconfig configObject)
    {
         //Intialize serial port with OnSerialPortReceived event handler.

    }

    public void GetStatusForMessage(string messageID)
    {
    }

    public void OnSerialPortReceived(string data)
    {
        if(data=="123")
              MessageSent(this,null);
        if(data=="456")
              StatusSent(this,null);
    }
}

This is a console application, it will be started when the system is started. Now we need a monitor application(basically a client with call back) when some event triggered on the event service, for that we should use WCF. and also the monitor application makes call to the service class.In the above eg GetStatusForMessage method will be invoked by the monitor application. So now how can we implement using WCF. If make the above class as service with the service contract, it wont be initialized and started until the client has initiate a call. This class object will be initialized and starts its function when ever the system restarts.

I found this article http://msdn.microsoft.com/en-us/magazine/cc163537.aspx. With this approach my service will become as publisher client and monitor application will become as subscriber client application. But the client has to make calls to the service class. So my client application should support both callbacks and also it should be able to call the service methods. How can I achieve this using WCF? Note the service class which is monitoring for events is a single instance and initialized when the application started.

Hope I ll get solutions for this. Please let me know for more clarifications.

2

There are 2 answers

0
Grzegorz W On
  1. Make your "serivce" a Widnows Service not a console app.
  2. You can make your MyService class a WCF service without any problems. But You can also create some other class to host Your service contract and simply communicate with your windows service implementation.
  3. There is no connection between windows service activation time and first WCF request ( this in not IIS, this is a self hosted WCF service, you start it when you want to).
  4. Here's link to self hosting WCF service tutorial
  5. Install your windows service on your machine with autorun option.
8
Richard Blewett On

Don't try to make your service classa WCF service. Make it a singleton and have WCF talk to it.

If you want the events to fire "events" to the monitoring application you will need to use a duplex binding (NetTcpBinding if cross machine or NetNamedPipeBinding on the same machine would be my recommendation). When the monitoring application connects save its callback channel and in the method wired up to the events call back on the callback channel.

Note you will have to keep sessions alive on both sides so the monitoring application and the service will have to fire something to each other more regularly than the configured receiveTimeout (10 minutes by default) but this can simply be a "ping" method to use as a keep-alive

I blogged about duplex communication a while back if it helps