2011-05-03 4 views
1

DataContractJsonSerializer에 대해 maxItemsInObjectGraph를 어떻게 설정합니까?DataContractJsonSerializer 및 maxItemsInObjectGraph

나는 번호 65536에서 온 않습니다 "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."

말하는 오류가 발생합니다. DataContractJsonSerializer에 대한 은 기본값이 Int32.MaxValue라고 표시합니다.

나는 행동 구성을 설정하려고 :

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

하지만 난과 같은 오류 얻을 : (<dataContractSerializer maxItemsInObjectGraph="500000"/>에 동작을 변경 "Invalid element in configuration. The extension name 'dataContractJsonSerializer' is not registered in the collection at system.serviceModel/extensions/behaviorExtensions."

이 오류를 제공하지 않습니다하지만 값을 변경하지 않는

클라이언트가 ChannelFactory로 만들어 졌으므로 여기서 설명한 ServiceBehavior 특성을 사용할 수 없습니다. here

+0

질문에서 dataContractSerializer 비트를 오른쪽으로 지나가고 대답을 제거하십시오. :) –

답변

2

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