DataContractJsonSerializer和maxItemsInObjectGraph[英] DataContractJsonSerializer and maxItemsInObjectGraph

本文是小编为大家收集整理的关于DataContractJsonSerializer和maxItemsInObjectGraph的处理方法,想解了DataContractJsonSerializer和maxItemsInObjectGraph的问题怎么解决?DataContractJsonSerializer和maxItemsInObjectGraph问题的解决办法?那么可以参考本文帮助大家快速定位并解决问题。

问题描述

如何设置 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 属性此处

解决方案

不知道能不能通过config实现(没试过),但是可以在代码上增加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();
    }
}

本文地址:https://www.itbaoku.cn/post/1794041.html