【问题标题】:DataContractJsonSerializer and maxItemsInObjectGraphDataContractJsonSerializer 和 maxItemsInObjectGraph
【发布时间】:2011-08-17 13:27:38
【问题描述】:

如何为 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 的 documentation 表示默认值为 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."

将行为更改为 &lt;dataContractSerializer maxItemsInObjectGraph="500000"/&gt; 不会出现错误,但不会更改值(这并不奇怪,因为我没有使用 dataContractSerializer)

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

【问题讨论】:

  • 跳过问题中的 dataContractSerializer 位,删除答案:)

标签: wcf webhttpbinding datacontractjsonserializer


【解决方案1】:

不知道能不能通过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();
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多