WCF DefaultBinding未在新的WsHttpBinding上使用

时间:2015-10-31 19:42:06

标签: wcf wcf-binding wshttpbinding wcf-wshttpbinding readerquotas

我有一个第三方库,它连接到特定端点的webservice。 对于某些服务,我得到以下例外:

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.

解决方法是,增加ReaderQuoata。 WCF通过配置文件建议这一点。

我的app.config现在看起来像:

<?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>

当我现在创建一个新的WsHttpBinding时,它的值仍然是默认值16384。

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

我缺少什么?

2 个答案:

答案 0 :(得分:1)

WSHttpBinding x = new WSHttpBinding();使用框架默认值创建WSHttpBinding的新实例。即使您在配置文件中定义了默认绑定配置,代码也不会使用它(请参阅WSHttpBinding.cs获取源代码)。换句话说,调用WSHttpBinding的无参数构造函数不会应用任何关联配置文件的默认绑定,至少我可以看到。

你有几个选择。首先,在配置文件中为绑定配置命名并引用它。其次,在以编程方式创建绑定时,将值分配给XmlDictionaryReaderQuotas

选项1

<bindings>
  <wsHttpBinding>
    <binding name="MyBinding">
      <readerQuotas maxNameTableCharCount="2147483647" />
    </binding>
  </wsHttpBinding>
</bindings>

WSHttpBinding x = new WSHttpBinding("MyBinding");

选项2

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

答案 1 :(得分:0)

您是否尝试过此链接https://social.msdn.microsoft.com/Forums/vstudio/en-US/17592561-c470-452a-a52c-2a5a2839582c/metadataexchangeclient-and-nametable-character-count-quota?forum=wcf

  

这是我们代码中的错误(您传递的绑定会被忽略   HTTP-GET)。有一些解决方法

     

你能看出他们中的任何一个是否适合你?