Friday, October 29, 2010

WCF 4.0 CommunicatinException

Found an exception sometimes in WCF 4.0 communication with wsHttpBinding binding in a test environment:

CommunictionException: "An error occurred while receiving the HTTP response to ....svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."

InnerExcpetion: "The underlying connection was closed: An unexpected error occurred on a receive."

InnerInnerException: "An existing connection was forcibly closed by the remote host"

NativeErrorCode: 10054

Turn on WCF logging in the server and found following error message using Microsoft Service Trace Viewer:

"There was an error while trying to serialize parameter http://tempuri.org/:Get...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."

Update server's web.config as suggested:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- ... -->
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
<!-- ... -->
</system.serviceModel>
Now get another exception in the client:
NetDispatcherFaultException: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:Get...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.
Inner Exception: "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. "

Update client's app.config:
<system.serviceModel>
<client>
<endpoint behaviorConfiguration="ServiceBehavior" _A_B_C_Setting... />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="ServiceBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
The exception is gone after configuration update.