0

I have a WCF service, with multiple clients connecting to it and subscribing for events.

I also have a Worker Thread that runs in the background, along with the hosted WCF service, and performs operations and pushes items into the 'Producer\Consumer Queue'.

How do I make the WCF service act when there are items in the queue, so that it can extract an item from the queue, analyze it - and publish a message to all the subscribed clients ?

I know that WCF service reacts to operations that are called from connected clients, but how do I make it respond when an item is queued into this queue ?

I would appreciate anyone that could answer with a simple code sample, if possible.

John Miner
  • 893
  • 1
  • 15
  • 32

2 Answers2

1

By simplest, you need an event to which your wcf service could susbscribe for enqueue event from the Queue and start processing the message once the event is raised. For a non generic queue you could override the Enqueue method to raise the OnChanged event.

public override void Enqueue(object obj)
{
    base.Enqueue(obj);
    OnChanged(EventArgs.Empty);
}

More details of how to raise event from generic queue is here

//from worker thread
queue.Enqueue(some_object);

//in wcf you could probably do something like this
queue.OnChanged +=  ProcessMessage;

public void ProcessMessage(object sender, EventArgs e)
{
     lock(lock_object)
     {
          var some_object = queue.Dequeue;
          //processing logic and broadcasting to client
     }
}
Community
  • 1
  • 1
Anand
  • 14,545
  • 8
  • 32
  • 44
  • yes, but where would the WCF service register for this event ? in the service's constructor ? and what happens if the event is raised when the service is processing a message from one of it's clients ? is this good programming ? doesn't seem like it ... – John Miner Jul 01 '12 at 22:30
  • 1
    Whenever you would work with multiple threads, race conditions are bound to happen. The onus lies on us, the programmers, to handle it(or simply choose not to do multi threaded application). If you see closely enough, I have used "lock" statement while dequeuing the items, to avoid race condition. I do not understand what is your concept of good programming. – Anand Jul 02 '12 at 04:46
  • 1
    To answer your " but where would the WCF service register for this event". Yes constructor seems a good place. (If I were you, I would make the queue static). I also hope your worker thread is spawned inside your wcf service.. – Anand Jul 02 '12 at 04:52
0

Here it is my idea, this might not be the best but it is a start. To achieve the desired functionality for your scenario create two WCF services. First of the services will be called QueueService which basically holds and manages your queue. Second service call it PublisherService, this one has as purpose to be notified by the QeueService when new items are added/removed from the queue. To achieve that you have to use duplex channels (apply publish subscribe pattern http://msdn.microsoft.com/en-us/library/ms752254.aspx), where PublisherService acts as a client for QueueService. Now about your clients which are going to consume the PublisherService... I am not sure what technique you have used until now. Are you using duplex channels or continuous streaming to get the data from the PublisherService? (http://blog.john-thiriet.com/en/2011/08/using-reactive-extensions-for-a-push-based-client-server-communication-with-silverlight-and-wcf/)

Mihai H
  • 3,291
  • 4
  • 25
  • 34