关闭

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

387人阅读 评论(0) 收藏 举报

出现此错误的原因是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;
                }
            }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:40832次
    • 积分:886
    • 等级:
    • 排名:千里之外
    • 原创:42篇
    • 转载:45篇
    • 译文:0篇
    • 评论:1条
    最新评论