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