当所有设置都处于最大值时,已超过传入消息的最大消息大小配额 (65536)

Maximum message size quota for incoming messages (65536) has been exceeded when all settings at their maximum values

对网络服务的调用正在return发送此消息:

System.ServiceModel.CommunicationException: The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.

我错过了什么吗?我相信我正在将所有相关的 WCF 消息大小参数设置为它们的最大可能值。我在客户端(调用 Web 服务的 Web 应用程序)和 Web 服务本身都有以下设置。

在两个配置文件中,这是唯一定义的绑定,客户端和服务端点中的选定绑定都具有此属性:

binding="netTcpBinding"

绑定标签包含:

<netTcpBinding>
    <binding name="netTcpBinding"  
             maxBufferSize="2147483647" 
             maxBufferPoolSize="2147483647"  
             maxReceivedMessageSize="2147483647">
         <readerQuotas maxDepth="2147483647" 
                       maxStringContentLength="2147483647"    
                       maxArrayLength="2147483647" 
                       maxBytesPerRead="2147483647" 
                       maxNameTableCharCount="2147483647" />
         ...
<serviceBehaivors...behavior>
<dataContractSerializer maxItemsInObjectGraph="2147483647" />

我使用了 DataContractSerializer 并手动序列化了导致上述错误的 return 值,如下所示:

DataContractSerializer dcs = new DataContractSerializer(typeof(List<MyObject>));
        using (Stream stream = new MemoryStream())
        {
            dcs.WriteObject(stream, list);
            Console.WriteLine(stream.Length);
        }

写入屏幕的值为“443235”,我猜这是 wcf 消息的近似大小。我不明白这个错误是从哪里来的,或者如何阻止它发生,欢迎任何建议。

您需要在 endpoint 元素中使用 bindingConfiguration 属性,并为其指定适当绑定配置的名称。所有 binding 告诉端点是 要使用的 类型的绑定,如果没有相关的 bindingConfiguration 它将使用绑定的默认值。

所以在你的端点,你会做这样的事情:

<endpoint address="" 
          binding="netTcpBinding" 
          bindingConfiguration="netTcpBinding" 
          contract="IMyService" />

现在您的端点将获取您在名为 "netTcpBinding" 的配置部分中为 NetTcpBinding 协议定义的值。

您在 WCF 的客户端配置上所做的是合适的,但在 WCF 服务端配置上不合适,您必须在服务端配置上使用自定义绑定来覆盖默认设置readerQuotas 或其他此类设置以覆盖服务端默认设置。

http://devproconnections.com/net-framework/custom-bindings-part-i

您创建自定义绑定并将 WCF 服务端配置指向自定义绑定