title: Convert web service configuration to code
body: <p>I have a client for SOAP service in a console application, I need to move the service client to sharepoint 2010. I do not want to do config files deployment and other sharepoint-related stuff, so I decided to hardcode binding information, the only configurable option is URL. But I encountred some troubles doing this. I have a config:</p>
  
  <pre><code>    &lt;system.serviceModel&gt;
  &lt;bindings&gt;
    &lt;customBinding&gt;
      &lt;binding name="SI_PMProjectMaintain_SOUTBinding"&gt;
        &lt;textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
          messageVersion="Soap11" writeEncoding="utf-8"&gt;
          &lt;readerQuotas maxDepth="10000000" maxStringContentLength="10000000"
            maxArrayLength="67108864" maxBytesPerRead="65536" maxNameTableCharCount="100000" /&gt;
        &lt;/textMessageEncoding&gt;
        &lt;httpTransport authenticationScheme="Basic" bypassProxyOnLocal="false"
          hostNameComparisonMode="StrongWildcard" keepAliveEnabled="false"
          proxyAuthenticationScheme="Basic" realm="XISOAPApps" useDefaultWebProxy="true" /&gt;
      &lt;/binding&gt;
    &lt;/customBinding&gt;
  &lt;/bindings&gt;
  &lt;client&gt;
    &lt;endpoint address="http://url/XISOAPAdapter/MessageServlet?senderParty=&amp;amp;senderService=Param1&amp;amp;receiverParty=&amp;amp;receiverService=&amp;amp;interface=interface&amp;amp;interfaceNamespace=url"
      binding="customBinding" bindingConfiguration="SI_PMProjectMaintain_SOUTBinding"
      contract="PmProjectMaintain.SI_PMProjectMaintain_SOUT" name="HTTP_Port" /&gt;
  &lt;/client&gt;
  </code></pre>
  
  <p></p>
  
  <p>Also, I have a code:</p>
  
  <pre><code>    var service = new ServiceClient();
      service.ClientCredentials.Windows.ClientCredential = new NetworkCredential("user", "password");
      service.ClientCredentials.UserName.UserName = "user";
      service.ClientCredentials.UserName.Password = "password";
  
      var resp = service.Operation();
      Console.WriteLine(resp.Response);
  </code></pre>
  
  <p>It works as expected. Now I want to get rid of xml config and completly move it to code:</p>
  
  <pre><code>public class CustomHttpTransportBinding : CustomBinding
  {
      public CustomHttpTransportBinding()
      {
      }
  
      public override BindingElementCollection CreateBindingElements()
      {
          var result = new BindingElementCollection();
          result.Add(new TextMessageEncodingBindingElement
          {
              MaxReadPoolSize = 64,
              MaxWritePoolSize = 16,
              MessageVersion = MessageVersion.Soap11,
              WriteEncoding = Encoding.UTF8,
  
              //ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas
              //{
              //    MaxDepth = 10000000,
              //    MaxStringContentLength = 10000000,
              //    MaxArrayLength = 67108864,
              //    MaxBytesPerRead = 65536,
              //    MaxNameTableCharCount = 100000
              //}
          });
  
          result.Add(new HttpTransportBindingElement
          {
              AuthenticationScheme = AuthenticationSchemes.Basic,
              BypassProxyOnLocal = false,
              HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard,
              KeepAliveEnabled = false,
              ProxyAuthenticationScheme = AuthenticationSchemes.Basic,
              Realm = "XISOAPApps",
              UseDefaultWebProxy = true
          });
  
          return result;
      }
  }
  </code></pre>
  
  <p>And I use it like this:</p>
  
  <pre><code>    var service = new ServiceClient(new CustomHttpTransportBinding(), new EndpointAddress(url));
      service.ClientCredentials.Windows.ClientCredential = new NetworkCredential("user", "password");
      service.ClientCredentials.UserName.UserName = "user";
      service.ClientCredentials.UserName.Password = "password";
  
      var resp = service.Operation();
      Console.WriteLine(resp.Response);
  </code></pre>
  
  <p>It reaches the server, but throws exception ("Server Error"), so I belive something wrong with my configuration. I can not specify the reader quotas also. I've tried loading <a href="http://blogs.msdn.com/b/jimmytr/archive/2011/02/15/snippet-construct-wcf-binding-from-string-xml.aspx" rel="nofollow">configuration from string</a>, but it is not worked for me, same error.
  Confi
  It seems that I can not convert custom bindings from XML to code in .net 3.5. Can somebody take a look on my code and confirm? Are there other ways to have a service client with custom binding in code? </p>