title: WCF server/client callbacks, reply from client to server
body: <p>In my client/server application, I want count other value in everyone client.
  I made application using callbacks, but something is wrong. I get Exception, when I want call method <code>pipeproxy.polacz(S);</code> Which get value to server and write in server console now. </p>
  
  <p>Exception is:</p>
  
  <p><code>This operation would deadlock because the reply cannot be received until the current Message completes processing. If you want to allow out-of-order message processing, specify ConcurrencyMode of Reentrant or Multiple on CallbackBehaviorAttribute.</code></p>
  
  <p>Other problem is, how sum resault in this funkction from all clients.
  example;</p>
  
  <pre><code>client 1: S = 1;  
  client 2: S = 2;  
  client 3: S = 3;
  </code></pre>
  
  <p>And this function  take result from all clients and sum it. So server will write 6 in server console.</p>
  
  <p>My application code:</p>
  
  <p>server:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.ServiceModel;
  using Interface;
  
  namespace WCFapp
  {
      class Program
      {
          static void Main(string[] args)
          {
              Klienci cust = new Klienci();
              cust.Connect();
          }
      }
  }
  </code></pre>
  
  <p>. </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Interface;
  
  namespace WCFapp
  {
       [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
  class Klienci : IMessage
  {
      private static List&lt;ImessageCallback&gt; subscribers =
          new List&lt;ImessageCallback&gt;();
  
      public void lista()
      {
          string nm = Console.ReadLine();
          if (nm == "1")
          {
              Console.WriteLine("Number of conected clients: " + subscribers.Count());
              funkcja();
  
          }
      }
  
      public void Connect()
      {
          using (ServiceHost host = new ServiceHost(
              typeof(Klienci), new Uri("net.tcp://localhost:8000")))
          {
              host.AddServiceEndpoint(typeof(IMessage),
                  new NetTcpBinding(), "ISubscribe");
  
              try
              {
                  host.Open();
                  lista();
                  Console.ReadLine();
                  host.Close();
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.Message);
              }
          }
      }
  
      public bool Subscribe()
      {
          try
          {
              ImessageCallback callback = OperationContext.Current.GetCallbackChannel&lt;ImessageCallback&gt;();
              if (!subscribers.Contains(callback))
                  subscribers.Add(callback);
              Console.WriteLine("Client is conected ({0}).", callback.GetHashCode());
              return true;
          }
          catch (Exception e)
          {
              Console.WriteLine(e.Message);
              return false;
          }
      }
  
      public bool Unsubscribe()
      {
          try
          {
              ImessageCallback callback = OperationContext.Current.GetCallbackChannel&lt;ImessageCallback&gt;();
              if (subscribers.Contains(callback))
                  subscribers.Remove(callback);
              Console.WriteLine("Client is unconected ({0}).", callback.GetHashCode());
              return true;
          }
          catch
          {
              return false;
          }
      }
  
      public void funkcja()
      {
          int a = 1; int b = 3;
          subscribers.ForEach(delegate(ImessageCallback callback)
          {
              if (((ICommunicationObject)callback).State == CommunicationState.Opened)
              {
              Console.WriteLine("a= {0} , b= {1}", a, b);
              callback.klient_licz(a, b);
              a++;
              b++;
              }
          });
  
      }
  
      public void polacz(int S)
      {
  
          Console.WriteLine("Sum: {0}", S);
      }
    }
  }
  </code></pre>
  
  <p>Interface:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.ServiceModel;
  
  
  namespace Interface
  {
       [ServiceContract(CallbackContract = typeof(ImessageCallback), SessionMode = SessionMode.Required)]
  public interface IMessage
  {
      [OperationContract]
      void funkcja();
  
      [OperationContract]
      void polacz(int S);
  
      [OperationContract]
      bool Subscribe();
  
      [OperationContract]
      bool Unsubscribe();
  
  }
  [ServiceContract]
  public interface ImessageCallback
  {
      [OperationContract]
      void klient_licz(int a, int b);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Client:</p>
  
  <pre><code>    using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.ServiceModel;
  using Interface;
  
  namespace Client
  {
      class Program
      {
          static void Main(string[] args)
          {
              clients cl = new clients();
              if (cl.Conect() == true)
              {
                  string tmp = Console.ReadLine();
                  while (tmp != "EXIT")
                  {
                      cl.SendMessage(tmp);
                      tmp = Console.ReadLine();
                  }
  
              }
               cl.Close();
               Environment.Exit(0);
         }
     }
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.ServiceModel;
  using Interface;
  
  namespace Client
  {
      class clients : ImessageCallback, IDisposable
      {
          IMessage pipeProxy = null;
          public bool Conect()
          {
              DuplexChannelFactory&lt;IMessage&gt; pipeFactory =
                  new DuplexChannelFactory&lt;IMessage&gt;(
                      new InstanceContext(this),
                      new NetTcpBinding(),
                      new EndpointAddress("net.tcp://localhost:8000/ISubscribe"));
              try
              {
                  pipeProxy = pipeFactory.CreateChannel();
                  pipeProxy.Subscribe();
                 return true;
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.Message);
                  return false;
              }
  
          }
  
          public void Close()
          {
              pipeProxy.Unsubscribe();
          }
  
  
          public void klient_licz(int a, int b)
          {
              int S = a + b;
              Console.WriteLine("Sum= {0}", S);
              pipeProxy.polacz(S); //ERROR
          }
  
      }
  }
  </code></pre>