Setting maximum object size in WCF

本文介绍了解决WCF服务中因返回数据过大而导致错误的方法。通过调整客户端和服务端的MaxItemsInObjectGraph属性,可以有效解决数据序列化限制问题。

Setting maximum object size in WCF

Posted: 2nd June 2010 by thegrayzone in Development
Tags: SilverlightWCF
1

I have recently been working on a Silverlight application that uses WCF for all data access functions.

The application worked fine on my development machine but when I moved it over to a test environment I received the following, rather vague, error when making a few of the database calls:

“The remote server returned an error: NotFound”

After some debugging I realised that this happened when data being returned to the Silverlight application was too large, I then found the inner exception was:

Maximum number of items that can be serialized or deserialized in an object graph is ’65536′. Change the object graph or increase the MaxItemsInObjectGraph quota

The fix for this involves changing the MaxItemsInObjectGraph property for the service in both the client and the server code.

Server Side

The server side change is done within the web.config file of the web application that is hosting the Silverlight application. Add the following tag to the behavior tag for the misbehaving service:

1 <dataContractSerializer maxItemsInObjectGraph="2147483647" />

This sets the maxItemsInObjectGraph = to Int32.Max. Now the fill behavior tag should look something like:

1 <system.serviceModel>
2   <behaviors>
3     <serviceBehaviors>
4       <behavior name="MyService.Service1Behavior">
5         <serviceMetadata httpGetEnabled="true" />
6         <serviceDebug includeExceptionDetailInFaults="true" />
7         <dataContractSerializer maxItemsInObjectGraph="2147483647" />
8       </behavior>
9 .....
Client Side

I made the client side change in the code behind page within my Silverlight application. I added a service reference to the above WCF service, Service1, and rather than just declaring a new instance of Service1 the way I was originally doing it, like:

1 private Service1Client myService = new Service1Client();

I altered the declaration of my Service1Client variable to be:

01 private Service1Client _myService;
02 private Service1Client myService
03 {
04   get
05   {
06     if (_myService == null)
07     {
08       BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
09       binding.MaxReceivedMessageSize = 2147483647;
10       Uri serviceUri = new Uri(App.Current.Host.Source, "../Service1.svc");
11  
12       _myService = new Service1Client(binding, new EndpointAddress(serviceUri));
13     }
14     return _myService;
15   }
16 }

In the above code I manually declare a BasicHttpBinding object and set it’s MaxReceivedMessageSize to Int32.Max (default 65,536).

Using Int32.Max for these values is slightly overkill but it shows how you can limit the amount of data being passed from your service.

http://thegrayzone.co.uk/blog/2010/06/setting-maximum-object-size-in-wcf/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值