wcfwcf-bindingwshttpbindingwcf-wshttpbindingreaderquotas

WCF DefaultBinding not used on new WsHttpBinding


I´m having a 3rd party library that connects to webservice at specific endpoints. With some services, i get the following exception:

Message=The maximum nametable character count quota (16384) has been exceeded while reading XML data. The nametable is a data structure used to store strings encountered during XML processing - long XML documents with non-repeating element names, attribute names and attribute values may trigger this quota. This quota may be increased by changing the MaxNameTableCharCount property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 1339.

The solution is, to increase the ReaderQuoata. WCF suggests this via config files.

My app.config now looks like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
    </startup>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding>
          <readerQuotas maxNameTableCharCount="2147483647" />
        </binding>
      </wsHttpBinding>
    </bindings>   
  </system.serviceModel>
</configuration>

When i now create a new WsHttpBinding, the value of it is still the default value of 16384.

    class Program
    {
        static void Main(string[] args)
        {
            WSHttpBinding x = new WSHttpBinding();
            Console.WriteLine(x.ReaderQuotas.MaxNameTableCharCount); //prints 16384 
        }
    }

What am i missing?


Solution

  • WSHttpBinding x = new WSHttpBinding(); creates a new instance of WSHttpBinding with the framework default values. Even though you have a default binding configuration defined in the configuration file, it is not being used by your code (See WSHttpBinding.cs for the source code). In other words, calling the parameterless constructor of WSHttpBinding does not apply the default binding from any associated config files, at least that I could see.

    You have a couple of options here. First, give a name for the binding configuration in your config file and reference that. Second, assign the values to the XmlDictionaryReaderQuotas when you create the binding programatically.

    Option 1

    <bindings>
      <wsHttpBinding>
        <binding name="MyBinding">
          <readerQuotas maxNameTableCharCount="2147483647" />
        </binding>
      </wsHttpBinding>
    </bindings>
    
    WSHttpBinding x = new WSHttpBinding("MyBinding");
    

    Option 2

    WSHttpBinding x = new WSHttpBinding();
    x.ReaderQuotas rq = new XmlDictionaryReaderQuotas();
    rq.MaxNameTableCharCount = Int32.MaxValue;