Search code examples
wcfdatacontractserializer

WCF service result causes browser to show 'page cannot be displayed' on some instances


I am working on a WCF service which returns a record set based on query string parameters (like, starting page, page size), like this:

/{service-url}/?{some-other-parms}&pageindex=0&pagesize=9999&pagecount=1

I recently encountered a scenario where the browser (Chrome, FF, IE9) returns "page cannot be displayed" when I request all records. And it appears to encounter an exception during serialization.

I tried to play around with the parameters and found out if I set &pagesize=1598, the result returns perfectly (as an XML on the browser). If I go beyond that, the error occurs. So it seemed there was an offending data in the record set it returns. However, if I adjust the parameters to actually return only that seemingly offending data, it returns the result perfectly. So I'm now suspecting it is somehow in the serialization process. The app uses DataContract on the record class it returns. But it does not appear to override the serialization process (or nothing I could see it doing). How can I fix this?

I have put a step in the service method to log somewhere the number of records it returns. And on instances where the browser shows "page cannot be displayed", the log is indicating that it returned a non-empty record set. So I'm guessing the serialization is causing the result to be invalid so the browser could not render.


Solution

  • I enabled trace logging for the ServiceModel as @NawedNabiZada mentioned. This eventually gave me more info on the exception that the service is throwing:

    System.Runtime.Serialization.SerializationException threw "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."

    So I had to add the dataContractSerializer entry to my web.config, under system.serviceModel:

    <system.serviceModel>
       ...
       <behaviors>
          <serviceBehaviors>
             <behavior name="default" >
                <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                ...
             </behavior>
           </serviceBehaviors>
       </behaviors>
       ...
    </system.serviceModel>
    

    For more info on maxItemsInObjectGraph, refer to this MSDN page.