WCF:Maximum number of items that can be serialized or deserialized in an object graph is '65536'.

原创 2015年11月19日 16:36:27

出现此错误的原因是WCF默认的序列化值不够,导致查询大数量的时候报错,解决的方法是修改序列化的值,可在配置文件中修改,或者在代码中修改:

1、改配置文件:

在behaviors中添加<dataContractSerializer maxItemsInObjectGraph="2147483647"/>,如下:

<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
          <!--序列化大小设置-->
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>

客户端配置文件也添加<dataContractSerializer maxItemsInObjectGraph="2147483647"/>

2、代码修改,在代理类中添加如下代码:

服务器端:

ServiceHost host = new ServiceHost(serviceType, uri);
foreach (IServiceBehavior behavior in host.Description.Behaviors)
{
if (behavior is ServiceBehaviorAttribute)
{
(behavior as ServiceBehaviorAttribute).MaxItemsInObjectGraph = int.MaxValue;
}
}

客户端:

ChannelFactory模式如下修改:

ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding);
foreach (OperationDescription op in channelFactory.Endpoint.Contract.Operations)
{
DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
{
dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
}
}

ClientBase模式做如下修改:

EndpointAddress address = new EndpointAddress("http://10.0.0.1/TestService.svc/PC");
            WSHttpBinding binding = new WSHttpBinding();

Service service = new Service(binding, address);

foreach (OperationDescription op in service .Endpoint.Contract.Operations)
            {


                DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior;
                if (dataContractBehavior != null)
                {
                    dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
                }
            }


相关文章推荐

WCF大数据量处理

最近在处理投产问题里,遇到酒店列表总展示不了数据,查找原因发现系统报了“格式化程序尝试对消息反序列化时引发异常: 尝 试对参数 http://tempuri.org/ 进行反序列化时出错: Sea...

What can I learn right now in just 10 minutes that could be useful for the rest of my life?

What can I learn right now in just 10 minutes that could be useful for the rest of my life?

《How hard can it be? Estimating the difficulty of visual search in an image》和 PPT

《How hard can it be? Estimating the difficulty of visual search in an image》发表于2016年CVPR上,这篇文章是老师推荐的...

Incorrect decrement of the reference count of an object that is not owned at this point by the calle

Incorrect decrement of the reference count of an object that is not owned at this point by the cal...

LeetCode 323. Number of Connected Components in an Undirected Graph

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes),...

LeetCode 323. Number of Connected Components in an Undirected Graph(连通分量)

原题网址:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ Given n ...
  • jmspan
  • jmspan
  • 2016年04月25日 02:50
  • 713

Is there a limit to the number of columns in an HBase row?

Quora上一个哥们提出下面这个问题,之前其实读过的不过没怎么在意comments,而且自己还亲自测试了一下这个问题确定当一个row很大的时候不会发生拆分的。今天又看了一遍,其实这里面已经包含了很多信...
  • macyang
  • macyang
  • 2011年03月04日 09:28
  • 1033
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:WCF:Maximum number of items that can be serialized or deserialized in an object graph is '65536'.
举报原因:
原因补充:

(最多只允许输入30个字)