Tuesday, December 23, 2014

WCF: Identifing late exceptions in the pipeline

Hi.
Recently I’ve run into  a strange WCF service issue. The logs in the service method were indicating a successful completion of the call, but the call was still failing and the clients were getting exceptions.
 The exception message was: “An error occurred while receiving the HTTP response to <serviceEndpoint>. 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).“.
The service API which was called had a return type (not void), and the type was serializable. So the only explanation I could come up with was that something wrong goes with serialization. THAT WAS IT!
Luckily it’s possible to simulate the same serialization what WCF does behind the scene, and verify the assumptions. So I did it: created a test method within the unit-test project, called the API being called by the client to get properly built result object, and then tried to serialize it:
[TestMethod]
public void GetDataUsingDataContract_SerializesResults()
{  Service1 serviceInstance = new Service1();
  CompositeType argument = new CompositeType();
  CompositeType result = serviceInstance.GetDataUsingDataContract(argument);

  using (MemoryStream stream = new MemoryStream())
  {
 
   DataContractSerializer serializer = new DataContractSerializer(typeof(CompositeType));
    serializer.WriteObject(stream, result);
  }
}
Note: you’ll need to add a reference to System.Runtime.Serialization.dll.
In my case the “serializer.WriteObject(stream, result);” call threw with the detailed exception.
 
You can download the full sample project: http://1drv.ms/1sJljCd
 
Hope this helps.