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 に答える