title: How do I enable compression for WCF when using simple TCP?
body: <p>I am using WCF to transmit information between two identical copies of a program on separate computers, with the approach demonstrated on a single computer in the code below.  I know that .NET 4.5 <a href="https://msdn.microsoft.com/en-us/library/aa751889(v=vs.110).aspx" rel="nofollow noreferrer">supports compression out of the box</a> and I see an XML implementation in <a href="https://stackoverflow.com/questions/15680087/">this question</a>, but I don't understand how to enable it in this case.  How can I modify the program below to use compression with the fewest/simplest changes?</p>
  
  <pre><code>// Reference to System.ServiceModel
  class Program
  {
      static void Main(string[] args)
      {
          var server = new DemoWcfServer();
          var handler = new DemoWcfHandler();
  
          Uri serviceUri = new Uri("net.tcp://localhost/SimpleWcfDemo");
          var binding = new NetTcpBinding();
          binding.MaxBufferSize = 100000000;
          binding.MaxReceivedMessageSize = 100000000;
  
          var host = new ServiceHost(server, serviceUri);
          host.AddServiceEndpoint(typeof(IDemoWcfServer), binding, "");
          host.Open();
  
          var cf = new DuplexChannelFactory&lt;IDemoWcfServer&gt;(handler, binding, new EndpointAddress(serviceUri));
          IDemoWcfServer serviceProxy = cf.CreateChannel();
  
          serviceProxy.DoStuff(BigString('T', 1000000));
          Console.ReadKey();
      }
  
      static string BigString(char c, int n)
      {
          var sb = new StringBuilder();
          for (int i = 0; i &lt; n; i++)
              sb.Append(c);
          return sb.ToString();
      }
  
      [ServiceContract(CallbackContract = typeof(IDemoWcfCallback))]
      public interface IDemoWcfServer
      {
          [OperationContract]
          void DoStuff(string content);
      }
  
      [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
      public class DemoWcfServer : IDemoWcfServer
      {
          public void DoStuff(string content)
          {
              Console.WriteLine("Content of " + content.Length + " length received remotely; processing...");
              IDemoWcfCallback callback = OperationContext.Current.GetCallbackChannel&lt;IDemoWcfCallback&gt;();
              Task.Run(async () =&gt;
              {
                  await Task.Delay(TimeSpan.FromSeconds(3));
                  callback.NotifyStuffComplete(BigString('R', 1000000));
              });
          }
      }
  
      public interface IDemoWcfCallback
      {
          [OperationContract]
          void NotifyStuffComplete(string content);
      }
  
      public class DemoWcfHandler : IDemoWcfCallback
      {
          public void NotifyStuffComplete(string content)
          {
              Console.WriteLine("Response of " + content.Length + " length received");
          }
      }
  }
  </code></pre>