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:


<bindings>
<basicHttpBinding>
<binding name="Carbon.CarbonServiceBinding"
allowCookies="true"
maxBufferPoolSize="2147483647"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
closeTimeout="00:10:00"
openTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00">
<readerQuotas maxDepth="64" 
maxStringContentLength="2147483647" 
maxArrayLength="2147483647" 
maxBytesPerRead="4096" 
maxNameTableCharCount="16384"/>
</binding>
</basicHttpBinding>
</bindings>


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

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?