WCF传输大数据量的问题,现在网上讨论的很多,大多是设置maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647"。
这些我也都设置了,但是今天在调试WCF的时候,当我用一个帐号进行操作一切OK。但当我用另外一个帐号操作的时候却报了错误
“对象图中可以序列化或反序列化的项目数目上限为“65536”,请更改对象图或增加 MaxItemsInObjectGraph 的配额”。
最后我一查数据库才发现先前帐号的数据只有几百条,而另一个帐号有1W多条,肯定是数据爆了。
下面就来说下如果在服务端和客户端设置MaxItemsInObjectGraph
WCF 服务端配置文件
<system.serviceModel>
<services>
<service behaviorConfiguration="BasicServiceBehavior" name="XXX.WCF.Product">
<endpoint address="" binding="customBinding" bindingConfiguration="CompressionChannel" contract="XXX.WCF.IProduct">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<extensions>
<bindingElementExtensions>
<add
type="WcfExtensions.ServiceModel.Configuration.CompressionElement,WcfExtensions.ServiceModel"
name="compression"/>
</bindingElementExtensions>
</extensions>
<bindings>
<customBinding>
<binding name="CompressionChannel" receiveTimeout="01:00:00" sendTimeout="01:00:00" >
<compression/>
<textMessageEncoding>
<readerQuotas maxDepth="1024"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647" />
</textMessageEncoding>
<httpTransport maxReceivedMessageSize="2147483647" />
</binding>
</customBinding>
<basicHttpBinding>
<binding name="BasicHttpWithoutSecurity" transferMode="StreamedResponse" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" receiveTimeout="01:00:00" sendTimeout="01:00:00" >
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxStringContentLength="2147483647"/>
<security mode="None">
<transport clientCredentialType="None"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="BasicServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
客户端配置文件
<system.serviceModel>
<extensions>
<bindingElementExtensions>
<add
type="WcfExtensions.ServiceModel.Configuration.CompressionElement,WcfExtensions.ServiceModel"
name="compression"/>
</bindingElementExtensions>
</extensions>
<bindings>
<customBinding>
<binding name="CompressionChannel" >
<compression/>
<textMessageEncoding>
<readerQuotas maxDepth="32"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"/>
</textMessageEncoding>
<httpTransport maxReceivedMessageSize="2147483647" />
</binding>
</customBinding>
<basicHttpBinding>
<binding name="BasicHttpBinding_IProduct" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://dev.xxx.com/Product.svc" behaviorConfiguration="BasicServiceBehavior"
binding="customBinding" bindingConfiguration="CompressionChannel"
contract="ProductService.IProdcut" name="BasicHttpBinding_IProduct" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="BasicServiceBehavior">
<callbackDebug includeExceptionDetailInFaults="true"/>
<callbackTimeouts transactionTimeout="00:10:00"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
这样服务器端和客户端配置后,就可以提高最大传输量了。