1

如何为 DataContractJsonSerializer 设置 maxItemsInObjectGraph?

我收到一条错误消息"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."

65536这个数字是从哪里来的。DataContractJsonSerializer的文档说默认值为 Int32.MaxValue。

我试图在行为配置中设置它:

 <endpointBehaviors>
    <behavior name="WebBehavior">
      <webHttp />
      <dataContractJsonSerializer maxItemsInObjectGraph="500000"/>
    </behavior>
 </endpointBehaviors>

但我收到如下错误:"Invalid element in configuration. The extension name 'dataContractJsonSerializer' is not registered in the collection at system.serviceModel/extensions/behaviorExtensions."

将行为更改<dataContractSerializer maxItemsInObjectGraph="500000"/>为没有错误但不会更改值(这不足为奇,因为我没有使用 dataContractSerializer)

客户端是使用 ChannelFactory 创建的,因此我无法使用此处描述的 ServiceBehavior 属性

4

1 回答 1

2

我不知道你是否可以通过配置来做到这一点(没有尝试过),但你可以在代码上增加 MaxItemsInObjectGraph 属性,它应该可以工作。在下面的示例中,如果我不增加它,则调用失败;否则它会成功。

public class StackOverflow_5867304_751090
{
    public class Product
    {
        public string Name { get; set; }
        public int Price { get; set; }
    }
    [ServiceContract]
    public interface ITest
    {
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        List<Product> GetProducts(int size);
    }
    public class Service : ITest
    {
        public List<Product> GetProducts(int size)
        {
            List<Product> result = new List<Product>();
            for (int i = 0; i < size; i++)
            {
                result.Add(new Product { Name = "Prod " + i, Price = i });
            }
            return result;
        }
    }
    static Binding GetBinding()
    {
        return new WebHttpBinding() { MaxReceivedMessageSize = int.MaxValue };
    }
    static void AddBehavior(ServiceEndpoint endpoint)
    {
        endpoint.Behaviors.Add(new WebHttpBehavior());
        foreach (var operation in endpoint.Contract.Operations)
        {
            DataContractSerializerOperationBehavior dcsob = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
            if (dcsob != null)
            {
                dcsob.MaxItemsInObjectGraph = 1000000;
            }
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
        AddBehavior(endpoint);
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress));
        AddBehavior(factory.Endpoint);
        ITest proxy = factory.CreateChannel();
        Console.WriteLine(proxy.GetProducts(100000).Count);

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
于 2011-05-26T13:38:06.097 回答