0
Answer

Advice on my WCF event logic

nishant singh

nishant singh

15y
2.4k
1
I have to make a event notification service in WCF. i made it using callback mechanism of WCF. when the service is running all the clients can subscribe to the service and recieve a Broadcast message from server whenever any event occurs.
        According to my implementation there is a  Delegate at server side:
        public delegate void CallbackDelegate<T>(T t);
        public static CallbackDelegate<string> MessageReceived; to which all clients subscribe like:
  
// these two lines subscribes the client to a single event and whenever event occurs all client recieves the data.
   IEventSystemCallback callback = OperationContext.Current.GetCallbackChannel<IEventSystemCallback>();
   MessageReceived += callback.OnMessageReceived;

but my requirement is to filter the data based on the IP of the client and send only selected data based on some other parameters.
I thought of a logic according to which only one client will subscribe at a time to the Delegate so that he only will receive the data.
 so what i did is:  checked the coming data from the server method which is suppose to raise the Delegate, found  the correct client and subscribe it , send the message and then again unsubscribe it like this:

  if (readerName == listReaderNames[i].ToString())  // my filtering to get the correct client
                        {
 
                            MessageReceived += objCall.OnMessageReceived;
                         
                            string message ="Sample Data";
                            if (MessageReceived != null)
                            {
                                MessageReceived(message);  // sends the message to the subscribed client
                                MessageReceived -= objCall.OnMessageReceived;  // removes the subscription so that next time only
                                                                                                              one client will be there.
                            }
                        }

is this the correct approach or is there any other better way to achieve non Broadcast client notification through WCF.
please give me some idea.