2011-05-03 45 views
1

如何設置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的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."

改變行爲<dataContractSerializer maxItemsInObjectGraph="500000"/>沒有給出錯誤,但不改變值(並不意外,因爲我沒有使用dataContractSerializer)

客戶端使用ChannelFactory創建,所以我不能使用這裏描述的ServiceBehavior屬性here

+0

Blew右過去的問題dataContractSerializer位,刪除答案:) – 2011-05-03 15:02:19

回答

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