问题
How can I set the maxItemsInObjectGraph for the DataContractJsonSerializer?
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."
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>
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."
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)
The client is created with a ChannelFactory so I can't use the ServiceBehavior attribute as described here here
回答1:
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();
}
}
来源:https://stackoverflow.com/questions/5867304/datacontractjsonserializer-and-maxitemsinobjectgraph