在WebService中,如果返回值过大,比如Array,List,DataSet等达到一定数量级的话,可能引发下面的异常:
1.受信Message Quota Size超过65536 (System.ServiceModel.CommunicationException)
解决方法:修改客户端配置文件app.config 的 maxReceivedMessageSize,maxBufferSize 两个属性。
但是,数量级继续增大,还会遇到新的问题:
2.MaxItemsInObjectGraph Quta Size超过65536(InnerException:System.Runtime.SerializationException)
解决方法(注意红色和加粗部分):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="NewBehavior">
<dataContractSerializer maxItemsInObjectGraph="65536000" />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="Service1Soap" 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="2147483647" 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://localhost:3511/Service1.asmx" behaviorConfiguration="NewBehavior"
binding="basicHttpBinding" bindingConfiguration="Service1Soap"
contract="WebSvc.Service1Soap" name="Service1Soap" />
</client>
</system.serviceModel>
</configuration>