WCF中如何修改MaxItemsInObjectGraph的限制

在WCF/WebService中,如果返回值过大,比如Array,List,DataSet等达到一定数量级的话,可能引发下面的异常:

WebService代码[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{

[WebMethod]
public List<CommonComplexObject> GetData(int number)
{
List<CommonComplexObject> data = new List<CommonComplexObject>();
for (int i = 0; i < number; i++)
{
CommonComplexObject obj = new CommonComplexObject()
{
GenerateTime = DateTime.Now,
ID = System.Guid.NewGuid().ToString(),
Sign = this.GetType().Namespace
};
data.Add(obj);
}
return data;
}
}

[Serializable]
public class CommonComplexObject
{
public string ID { get; set; }
public DateTime GenerateTime { get; set; }
public string Sign { get; set; }
}

Client端代码int number = Convert.ToInt32(this.textBox1.Text);
WebSvc.Service1SoapClient client = new WebSvc.Service1SoapClient();
this.dataGridView1.DataSource = client.GetData(number);

1. 受信Message Quota Size超过65536 (System.ServiceModel.CommunicationException)

err1

解决方法:修改客户端配置文件app.configmaxReceivedMessageSize,maxBufferSize 两个属性。

但是,数量级继续增大,还会遇到新的问题:

2. MaxItemsInObjectGraph Quta Size超过65536(InnerException:System.Runtime.SerializationException)

err2

因为关联配置节点比较多,这个时候就得动用 WCF Service Configuration Edition 来修改了。

tool1

(1) 打开app.config 修改 Advanced 节点,为 Endpoint Behaviors 添加一个新的Behavior:“dataContractSerializer”, 修改该属性“MaxItemsInObjectGraph”的值

config2

config3

(2) 修改Client节点的Endpoints的Service1Soap的BehaviorConfiguration关联上刚才增加的NewBehavior

config4

(3) 保存

生成XML如下,其中红色部分是被修改的部分:
<behavior name="NewBehavior">
<dataContractSerializer maxItemsInObjectGraph="65536000" />
</behavior>

...

<endpoint address="http://localhost:3511/Service1.asmx" behaviorConfiguration="NewBehavior"
binding="basicHttpBinding" bindingConfiguration="Service1Soap"
contract="WebSvc.Service1Soap" name="Service1Soap" />

<?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="65536000" maxBufferPoolSize="524288" maxReceivedMessageSize="65536000" 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值