System.ServiceModel.CommunicationException: The socket connection was aborted.

I recently encountered this misleading error from WCF:

System.ServiceModel.CommunicationException: The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was ’00:00:59.9090000′. —>  System.IO.IOException: The read operation failed, see inner exception. —>  System.ServiceModel.CommunicationException: The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was ’00:00:59.9090000′. —>  System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host.

I got this when calling a WCF service (S1), which in turned called another WCF service (S2). It turned out that the root cause was a bad schema. The S2 operation GetData returned a string:

<wsdl:message name="NaPersonPostXMLWS_getData_OutputMessage">
  <wsdl:part name="getDataReturn" type="xsd:string" />
</wsdl:message>

This string was in fact an XML document of a known schema, which caused the problem. Here is an extract of the schema:

<xs:complexType name="CivilstandTYPE">
    <xs:sequence>
        <xs:element name="CivilstandKod" nillable="true" minOccurs="0"/>
        …

Note that the CivilstandKod element has no type. Therefore, XSD.exe generated a field of type object:

public partial class CivilstandTYPE
{
    private object civilstandKodField;
    …

S2 returned valid XML:


<Civilstand>
  <CivilstandKod>OG</CivilstandKod>
</Civilstand>

But when S1 deserialized this, I discovered that civilstandKodField became an instance of type XmlNode[], i.e. an array of XmlNodes, and civilstandKodField[0].Value was “OG”. When S1 was about to return the response, it had to serialize the object structure again to XML, and obviously didn’t know what to do with this field, which generated the error.

Changing the schema to

<xs:complexType name="CivilstandTYPE">
    <xs:sequence>
        <xs:element name="CivilstandKod" type="CivilstandKodTYPE" nillable="true" minOccurs="0"/>
        …

and regenerating the proxy classes fixed the problem!

Advertisements