title: Set ReaderQuotas.MaxStringContentLength, in WCF via reflection WSHttpBinding
body: <p>I need to consume WCF when I don't know the type of binding, it could be BasicHttpBinding or WSHttpBinding. I created a simple solution to test without reflection and it works. But when I try use it in another project via reflection, when I use WSHttpBinding, I get this exception (Spanish):</p>
  
  <pre><code>{System.Xml.XmlException: Se superó la cuota de longitud del contenido de cadena (8192) al leer los datos XML. Esta cuota se puede aumentar cambiando la propiedad MaxStringContentLength en el objeto XmlDictionaryReaderQuotas que se usa para crear el lector XML. Línea 1, posición 10572.
     en System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
     en System.Xml.XmlDictionaryReader.ReadContentAsString(Int32 maxStringContentLength)
     en System.Xml.XmlBaseReader.ReadContentAsString()
     en System.Xml.XmlBaseReader.ReadElementContentAsString()
     en System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.PartInfo.ReadValue(XmlDictionaryReader reader)
     en System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeParameter(XmlDictionaryReader reader, PartInfo part)
     en System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeResponse(XmlDictionaryReader reader, Object[] parameters)
     en System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeReply(Message message, Object[] parameters)}
  </code></pre>
  
  <p>in BasicHttpBinding it works ok, (when I configure WCF with this binding)</p>
  
  <p>I put in the web.config this code in WsHttpBinding server</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.0" /&gt;
    &lt;/system.web&gt;
    &lt;system.serviceModel&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors &gt;
          &lt;behavior name="ServiceBehaviors"  &gt;
            &lt;!-- To avoid disclosing metadata information, 
            set the value below to false and remove the metadata endpoint above before deployment --&gt;
            &lt;serviceMetadata httpGetEnabled="True"/&gt;
            &lt;!-- To receive exception details in faults for debugging purposes, 
            set the value below to true.  Set to false before deployment 
            to avoid disclosing exception information --&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="False" /&gt;
            &lt;serviceCredentials&gt;
              &lt;serviceCertificate findValue="localhost" x509FindType="FindBySubjectName"
                               storeLocation="LocalMachine" storeName="My" /&gt;
              &lt;userNameAuthentication userNamePasswordValidationMode="Custom"
               customUserNamePasswordValidatorType="TestService.CustomValidator, TestService" /&gt;
            &lt;/serviceCredentials&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;bindings&gt;
        &lt;wsHttpBinding&gt;
          &lt;binding closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00"
            maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"&gt;
            &lt;readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
              maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
            &lt;security mode="Message"&gt;
              &lt;transport clientCredentialType="None" /&gt;
              &lt;message clientCredentialType="None" /&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/wsHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;services&gt;
        &lt;service behaviorConfiguration="ServiceBehaviors" name="TestService.Service1"&gt;
          &lt;endpoint binding="wsHttpBinding" contract="TestService.IService1" /&gt;
          &lt;endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" /&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt;
    &lt;/system.serviceModel&gt;
    &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
    &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>This works without reflection, to test this server I put in app.config client this simple code:</p>
  
  <pre><code>&lt;bindings&gt;
              &lt;wsHttpBinding&gt;
                  &lt;binding name="WSHttpBinding_IService1"&gt;
                      &lt;readerQuotas maxStringContentLength="2147483647" /&gt;
                      &lt;security&gt;
                          &lt;message clientCredentialType="None" /&gt;
                      &lt;/security&gt;
                  &lt;/binding&gt;
              &lt;/wsHttpBinding&gt;
          &lt;/bindings&gt;
  </code></pre>
  
  <p>but I can't put this in my other project because the WCF could change.
  I have this code in C# to configure the dynamic web service, but it doesn't work:</p>
  
  <pre><code>            PropertyInfo channelFactoryProperty = proxyInstance.GetType().GetProperty("ChannelFactory");
  
              if (channelFactoryProperty == null)
              {
                  throw new InvalidOperationException("There is no ''ChannelFactory'' property on the DomainClient.");
              }
              ChannelFactory factory = (ChannelFactory)channelFactoryProperty.GetValue(proxyInstance, null);
  
              factory.Endpoint.Binding.SendTimeout = new TimeSpan(0, 10, 0);
              factory.Endpoint.Binding.OpenTimeout = new TimeSpan(0, 10, 0);
              factory.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
              factory.Endpoint.Binding.CloseTimeout = new TimeSpan(0, 10, 0);
  
              PropertyInfo channelFactoryPropert = proxyInstance.GetType().GetProperty("InnerChannel");
              System.ServiceModel.IClientChannel factor = (System.ServiceModel.IClientChannel)channelFactoryPropert.GetValue(proxyInstance, null);
              factor.OperationTimeout.Add(new TimeSpan(0, 10, 0));
              factor.OperationTimeout = new TimeSpan(0, 10, 0);
  
              switch ((factory.Endpoint.Binding).GetType().ToString())
              {
                  case "System.ServiceModel.BasicHttpBinding":
                      BasicHttpBinding _basicBinding = (BasicHttpBinding)factory.Endpoint.Binding;
                      _basicBinding.MaxBufferPoolSize = 2147483647;
                      _basicBinding.MaxBufferSize = 2147483647;
                      _basicBinding.MaxReceivedMessageSize = 2147483647;
                      _basicBinding.OpenTimeout = new TimeSpan(0, 10, 0);
                      break;
  
                  case "System.ServiceModel.WSHttpBinding":
                      WSHttpBinding _wsBinding = (WSHttpBinding)factory.Endpoint.Binding;                     
                      _wsBinding.MaxBufferPoolSize = 2147483647;
                      _wsBinding.MaxReceivedMessageSize = 2147483647;
                      _wsBinding.OpenTimeout = new TimeSpan(0, 10, 0);
                      _wsBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
  
  
                      XmlDictionaryReaderQuotas _wsBindingRQ = (XmlDictionaryReaderQuotas)_wsBinding.ReaderQuotas;
                      _wsBindingRQ.MaxArrayLength = 2147483647;
                      _wsBindingRQ.MaxBytesPerRead = 2147483647;
  
                      _wsBindingRQ.MaxNameTableCharCount = 2147483647;
                      _wsBindingRQ.MaxStringContentLength = 2147483647;
                      break;
              } 
  </code></pre>
  
  <p>I don't know what code to configure in C# in the app.config in this project, it is empty.</p>