CubiSoft Solutions

How to Set MaxItemsInObjectGraph Programmatically for Client

Recently I was working on a sharepoint project. The sharepoint webpart we were preparing for deployment relied on an Azure Web Service role and was using a “usual” WCF generated proxy client to connect to aforementioned role. However, the catch was that the client was not allowed to use a configuration file so every time it was initialized, the binding elements were programmatically set.

Everything was working flawlessly until the day came and we received the dreadful “MaxItemsInObjectGraph” error message.

“The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://compusight.com/:<MethodName>Result. The InnerException message was ‘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. ‘. Please see InnerException for more details.”

The fix was “easy”; we would simply increase the quota of the DataContractSerializer in the server side and the client side. For the service host we added the service behavior:

<serviceBehaviors>

<behavior name="Compusight.Services.ServiceBehavior">

<serviceMetadata httpGetEnabled="true" />

<dataContractSerializer maxItemsInObjectGraph="2147483647" />

<serviceDebug includeExceptionDetailInFaults="true" />

</behavior>

</serviceBehaviors>

Finally we were going to add the client endpoint behavior for the DataContractSerializer.

<endpointBehaviors>

<behavior name="Compusight.Services.ServiceEndpointBehavior">

<dataContractSerializer maxItemsInObjectGraph="2147483647" />

</behavior>

</endpointBehaviors>

But, hold on. As I recall, there was no configuration file for the client. This was unfortunately the case. After searching all over internet, finally came to the MSDN page that suggests adding the DataContractSerializerOperationBehavior which was supposed to be added to the ServiceHost as follows:

But adding it to the client was not at all described. And actually, naively enough, calling “Find <DataContractSerializerOperationBehavior> ();” from the client.Endpoint was throwing the null exception repeatedly.

Finally realizing that this is an operation contract, decided to write an endpoint behavior and modify the operation contracts adding the desired OperationBehavior accordingly.

Can Bilgin works in CompuSight Corp as a Senior Software Architect. He has been working in software industry with Microsoft technologies for over a. In this period he took key roles in projects for high profile clients such as Microsoft, General Electric, Raiffeisen Bank, Jettainer and Adobe Echosign using technologies such as BizTalk, SharePoint, InfoPath, Windows Mobile, Windows Phone, WinRT and WCF.He is especially interested in Windows 8 (WinRT) platform and windows phone development. He tries to share his experience on his blog and through different MSDN activities. He regularly attends local Microsoft conferences and community events in the Balkan region.He is a Microsoft MVP with Windows Platform Development expertise.His team recently received the Best Productivity App Award from Windows Store Application Contest by Qualcom.