DataContractJsonSerializer和maxItemsInObjectGraph [英] DataContractJsonSerializer and maxItemsInObjectGraph

查看:137
本文介绍了DataContractJsonSerializer和maxItemsInObjectGraph的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为DataContractJsonSerializer设置maxItemsInObjectGraph?

How can I set the maxItemsInObjectGraph for the DataContractJsonSerializer?

我收到一条错误消息,说可以序列化或反序列化的最大项目数对象图中的 65536。更改对象图或增加MaxItemsInObjectGraph配额。

I get an error saying "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。

Where does the number 65536 come from. The documentation for DataContractJsonSerializer says the default is Int32.MaxValue.

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

I tried to set it in the behavior configuration:

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

但出现类似以下错误:配置中的无效元素。扩展名名称'dataContractJsonSerializer'未在system.serviceModel / extensions / behaviorExtensions的集合中注册。

but I get an error like: "Invalid element in configuration. The extension name 'dataContractJsonSerializer' is not registered in the collection at system.serviceModel/extensions/behaviorExtensions."

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

Changing the behavior to <dataContractSerializer maxItemsInObjectGraph="500000"/> gives no error but doesn't change the value (which is no surprise since I'm not using the dataContractSerializer)

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

The client is created with a ChannelFactory so I can't use the ServiceBehavior attribute as described here here

推荐答案

我不知道您是否可以通过config进行操作(不是尝试),但是您可以在代码上增加MaxItemsInObjectGraph属性,它应该可以工作。在下面的示例中,如果我不加价,则通话会失败;

I don't know if you can do it via config (haven't tried), but you can increase the MaxItemsInObjectGraph property on the code, and it should work. In the example below, if I don't increase it, the call fails; otherwise it succeeds.

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();
    }
}

这篇关于DataContractJsonSerializer和maxItemsInObjectGraph的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆