none
How to implement callback in wcf service fot chat application? RRS feed

  • Question

  • Hi,

      I m working on a wpf project with wcf service.I m working on a chat application using call back in wcf.In my project i can send message from one client application to other client application in one machine.But i need to send message one client app of a machine to another client app on another machine.Can u plz help me out My service code is as below....

    Code of service1.cs

    namespace DuplexWcfService
    {
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
        public class DuplexService : IDuplexService
        {
            private static Dictionary<string, IDuplexCallback> clients =
                     new Dictionary<string, IDuplexCallback>();
            private static object locker = new object();
    
            public void RegisterClient(string clientName)
            {
                if (clientName != null && clientName != "")
                {
                    try
                    {
                        IDuplexCallback callback =
                            OperationContext.Current.GetCallbackChannel<IDuplexCallback>();
                        lock (locker)
                        {
                            //remove the old client
                            if (clients.Keys.Contains(clientName))
                                clients.Remove(clientName);
                            clients.Add(clientName, callback);
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
    
            public void NotifyServer(EventDataType eventData)
            {
                lock (locker)
                {
                    var inactiveClients = new List<string>();
                    foreach (var client in clients)
                    {
                        if (client.Key != eventData.ClientName)
                        {
                            try
                            {
                                client.Value.BroadcastToClient(eventData);
                            }
                            catch (Exception ex)
                            {
                                inactiveClients.Add(client.Key);
                            }
                        }
                    }
    
                    if (inactiveClients.Count > 0)
                    {
                        foreach (var client in inactiveClients)
                        {
                            clients.Remove(client);
                        }
                    }
                }
            }
        }
            
    }

    code of Iservice1.cs

    namespace DuplexWcfService
    {
        [ServiceContract(SessionMode=SessionMode.Required, CallbackContract=typeof(IDuplexCallback))]
        public interface IDuplexService
        {
    
            [OperationContract(IsOneWay = true)]
            void RegisterClient(string clientName);
    
            [OperationContract(IsOneWay = true)]
            void NotifyServer(EventDataType eventData);
        }
        [DataContract()]
        public class EventDataType
        {
            [DataMember]
            public string ClientName { get; set; }
            [DataMember]
            public string EventMessage { get; set; }
        }
    }

    Code for callback service

    public interface IDuplexCallback
        {
           
                [OperationContract(IsOneWay = true)]
                void BroadcastToClient(EventDataType eventData);
           
        }

    plz help me out as asap

    Thanks


       

    Thursday, July 31, 2014 12:04 PM

Answers