WCF Service: Getting to the cause of error

If you are using WCF service as your mode of communication between client-server applications (and why shouldn’t you, this has been a great source of communication available since .NET 3.5), then there are chances that something wrong happened and you get a generic Communication Exception at your client. This post will highlight how to find the cause of such error.

The exception raised sometimes in our WPF/Web/WinForms client is:

System.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly.

Silverlight guys should be able to recall the famous NotFound exception that was thrown every time anything was wrong. Well, the above communication exception is typically raised when something was wrong at server and it was forced to terminate the service request. The best way to debug such exceptions is to enable WCF Tracing. This is dead simple and requires the following addition to web.config under configuation section:

<system.diagnostics>
   <sources>
         <source name="System.ServiceModel"
                 switchValue="Information, ActivityTracing"
                 propagateActivity="true">
         <listeners>
            <add name="traceListener"
                type="System.Diagnostics.XmlWriterTraceListener"
                initializeData="wcf_service_traces.svclog" />
         </listeners>
      </source>
   </sources>
</system.diagnostics>

Now we will get a file named: wcf_service_traces.svclog that can be opened via SvcTraceViewer.exe (included with Visual Studio installation). Typically we don’t need to worry about locating the file as .svclog extension will automatically be associated with that utility. In the utility, look out for error rows highlighted in red:

For my case, most of the errors were related to DataContractSerializer that was unable to serialize my business entities. So I wrote a quick method to test if an object can be serialized or not. Here’s the method that was called from each of my unit tests.

public static void TestSerialization<T>(T objectToSerialize)
{
    DataContractSerializer serializer = new DataContractSerializer(typeof(T));
    using (MemoryStream stream = new MemoryStream())
    {
        //will throw an exception if cant serialize
        serializer.WriteObject(stream, objectToSerialize);
    }
}

The above method will throw an exception if the entries cannot be serialized and we can look into the exception details to find the actual error. I hope you will find this method and post useful.

About these ads

2 Responses to “WCF Service: Getting to the cause of error”

  1. DotNetShoutout Says:

    WCF Service: Getting to the cause of error « Mehroz’s Experiments…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  2. DataContractSerializer: Working with class inheritence and circular references « Mehroz’s Experiments Says:

    [...] method from a client, it will throw a communication exception. Using the test method described in this post, we can see that the DataContractSerializer was unable to serialize the result throwing a [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 52 other followers

%d bloggers like this: