I have a RESTful WCF service and I get the following error: The maximum number of elements that can be serialized or deserialized in the object graph is "65536". Change the object graph or increase the quota of MaxItemsInObjectGraph.
I thought I solved it, but apparently not. Here is my code:
I am using a .SVC file that uses a custom factory as follows:
<%@ ServiceHost Language="C#" Debug="true" Service="myService" Factory="myCustomWebServiceHostFactory" %>
Here is the code for custom factory
public class myCustomWebServiceHost : WebServiceHost { public myCustomWebServiceHost() { } public myCustomWebServiceHost(object singletonInstance, params Uri[] baseAddresses) : base(singletonInstance, baseAddresses) { } public myCustomWebServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses) { } protected override void OnOpening() { foreach (var endpoint in Description.Endpoints) { var binding = endpoint.Binding as WebHttpBinding; if (binding != null) { const int fiveMegaBytes = 5242880; binding.MaxReceivedMessageSize = fiveMegaBytes; binding.MaxBufferSize = fiveMegaBytes; binding.MaxBufferPoolSize = fiveMegaBytes; } } base.OnOpening(); } } class myCustomWebServiceHostFactory : WebServiceHostFactory { protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { return new myCustomWebServiceHost(serviceType, baseAddresses); } }
Services:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceContract] [ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)] public class myService { ...service implementation code goes here }
Client:
public class myClient { WebChannelFactory<IMyService> cf; IMyService channel; public myClient() { WebHttpBinding _binding = new WebHttpBinding(); _binding.MaxBufferPoolSize = 5000000; _binding.MaxBufferSize = 5000000; _binding.MaxReceivedMessageSize = 5000000; _binding.TransferMode = TransferMode.Streamed; _binding.ReceiveTimeout = new TimeSpan(0, 0, 30); _binding.ReaderQuotas.MaxArrayLength = 5000000; Uri _uri = new Uri("http://myserviceurl"); cf = new WebChannelFactory<IMyService>(_binding, _uri); channel = cf.CreateChannel(); foreach (OperationDescription op in cf.Endpoint.Contract.Operations) { var dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>(); if (dataContractBehavior != null) { dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue; } } } ...client implementation code goes here }
UPDATE: I did a bit of work. It seems that serialization partially works on the server. I can make a GET from a browser, and I get back all the data in XML format. So this is normal. This seems to be part of the deserialization that is causing the error. If you look above in the myClient code, you will see how I try to set the MaxItemsInObjectGraph property for the DataContractSerializer behavior. Am I doing it right?
source share