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>
這樣伺服器端和用戶端配置後,就可以提高最大傳輸量了。