Xaml Workshop Pages

Tuesday, September 21, 2010

WCF Error: The underlying connection was closed: The connection was closed unexpectedly.

CommunicationException was unhandled
The underlying connection was closed: The connection was closed unexpectedly.

Once in a while I have come across this error while creating and testing a new WCF service.  And, after hours of searching for remedies, I have finally found it.

First let's start off with some "solutions" that didn't work.  While it was necessary for me to increase the limits on the Service and Client side configs, that was not the issue.  Nevertheless, for my uses, these were good add-ins:

In my Web.config file I added the following settings, the pertinent data in yellow:

<binding name="Carbon.CarbonServiceBinding"
<readerQuotas maxDepth="64" 

<behavior name="Carbon.CarbonServiceBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
<serviceTimeouts transactionTimeout="00:10:00"/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>

This did not fix the issue and I kept getting the closed connection exception.  The issue was discovered when I began looking into the class that was being returned by my web method and its Data Contract and Members.

My class, named ReturnContainer had a GUID, bool and some strings all marked as Data Member.  However there was one member of type object.  During some of the processing that gets done, an XElement is assigned to this object member.  And, apparently WCF does not know how to serialize an XElement.  The solution, for good measure, was 2 fold:

  1. Change object data type to string
  2. Change all methods that assign to the member to include a .ToString() when necessary
In a nutshell, this error can be caused by large data sets, but in my case, was caused by a failure to serialize certain data types.

No comments:

Post a Comment

What tutorial should be next?