title: Creating Custom Binding programmatically
body: <p>We need compression in our WCF Web Service and have already found a good solution:</p>
  
  <p><a href="http://www.codeproject.com/Articles/53718/Extending-WCF-Part-II" rel="nofollow">http://www.codeproject.com/Articles/53718/Extending-WCF-Part-II</a></p>
  
  <p>There's only one problem. Configuration files are no option. How those Custom Bindings can be created in code?</p>
  
  <pre><code>  &lt;customBinding&gt;
      &lt;binding name="ZipBinding" closeTimeout="00:10:00" 
  openTimeout="00:10:00" receiveTimeout="00:10:00" 
      sendTimeout="00:10:00"&gt;
        &lt;customMessageEncoding innerMessageEncoding="mtomMessageEncoding" 
      messageEncoderType="YourAssemblyName.YourMessageEncoder, 
      WcfExtensions"&gt;
          &lt;readerQuotas maxDepth="999999999" 
              maxStringContentLength="999999999" 
      maxArrayLength="999999999" maxBytesPerRead="999999999" 
      maxNameTableCharCount="999999999"&gt;
          &lt;/readerQuotas&gt;
        &lt;/customMessageEncoding&gt;
        &lt;httpTransport maxBufferSize="999999999" 
          maxReceivedMessageSize="999999999" 
      authenticationScheme="Anonymous" 
      proxyAuthenticationScheme="Anonymous" useDefaultWebProxy="true"/&gt;
      &lt;/binding&gt;
    &lt;/customBinding&gt;
  </code></pre>
  
  <p>This is what i've come up with:</p>
  
  <pre><code>MessageEncodingBindingElementExtension customMessageEncoding = new MessageEncodingBindingElementExtension
  {
      InnerMessageEncoding = "binaryMessageEncoding",
      MessageEncoderType = "WcfExtensions.GZipMessageEncoder, WcfExtensions",
  };
  customMessageEncoding.ReaderQuotas.MaxDepth = 999999999;
  customMessageEncoding.ReaderQuotas.MaxStringContentLength = 999999999;
  customMessageEncoding.ReaderQuotas.MaxArrayLength = 999999999;
  customMessageEncoding.ReaderQuotas.MaxBytesPerRead = 999999999;
  customMessageEncoding.ReaderQuotas.MaxNameTableCharCount = 999999999;
  
  CustomBinding zipBinding = new CustomBinding(customMessageEncoding, new HttpTransportBindingElement
  {
      MaxBufferSize = 999999999,
      MaxReceivedMessageSize = 999999999,
      AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous,
      ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous,
      UseDefaultWebProxy = true
  });
  
  zipBinding.CloseTimeout = new TimeSpan(0, 10, 0);
  zipBinding.OpenTimeout = new TimeSpan(0, 10, 0);
  zipBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);
  zipBinding.SendTimeout = new TimeSpan(0, 10, 0);
  </code></pre>
  
  <p>But for some reason CustomBinding doesn't accept customMessageEncoding...</p>