0

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.

Sudhakar B
  • 1,465
  • 9
  • 16

2 Answers2

0

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

Richard Blewett
  • 6,089
  • 1
  • 18
  • 23
  • Hi, I have seen your blog, its a good blog. If I make it as singleton, the oject will be created when the client is requested right? but in my case the object is already created. So i need to create a WCF service which will talk to it. So how can a singleton WCF object should communicate with my already created service object. – Sudhakar B May 16 '12 at 11:16
  • Sorry - not a WCF singleton but a singleton in the design patterns sense - you can get it created at started a number of ways - remember I said not to make your service a WCF service. In fact a WCF singleton in created when the ServiceHost opens not on the first request - but I wouldn't tie your service class to WCF – Richard Blewett May 16 '12 at 12:03
  • Oh! OK. Even I also dont want to tie my WCF service class to my service class. Now my problem is how to interact with my WCF service class object and my service class.Need to have a global object of my service class so that I can access it in WCF singleton? (I don't think thats the good idea). – Sudhakar B May 16 '12 at 12:08
  • The WCF service class with have to have knowledge of the service class even via an abstraction (which will make it easier to unit test). The singleton pattern in .NET is well documented http://msdn.microsoft.com/en-us/library/ee816881.aspx for example. in that article you want to go for the static initializationmodel and make sure you have he service class type in your start up code. This will cause the static initializer to run and so create your object at startup – Richard Blewett May 16 '12 at 16:10
  • OK.Thank you. Is that link is correct? It is taking me to . Actually in the program.cs I ll start my application service and also I ll start the singlton WCF service.So now how do I access the object in the program.cs in the single ton WCF service for interaction (Thats where I am really stuck). – Sudhakar B May 17 '12 at 04:15
  • ah sorry - must have failed to copy the link and ended up with the one that was in the clipboard - this is the correct link http://msdn.microsoft.com/en-us/library/ff650316.aspx. You access the singleton via a static helper (as in the article linked) – Richard Blewett May 17 '12 at 06:44
  • Hi, Thank you for the link. I have developed a simple application using WCF which satisfies my requirements. As you said I have created the singleton WCF service class and singleton service object. I am setting the reference of application service to WCF singleton service object at the time of initialization. I think this is the correct method ? do you suggest any other method ? Now suppose if we implement per session WCF service object,then what is the best method to access my application service object inside WCF per session object ? Just by having static member which returns instance ? – Sudhakar B May 17 '12 at 07:44
  • The instancing model in WCF doesn;t really make any difference as far as your singleton is concerned - just use a static accessor as in the article. However, with a singleton WCF service be aware that you will only handle one request at a timne unless you change the ConcurrencyMode to Multiple. If you do this you will be responsible for making the code thread safe – Richard Blewett May 17 '12 at 08:50
0
  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.
Grzegorz W
  • 3,487
  • 1
  • 21
  • 21