David Foderick's Blog - OnMaterialize()

Debugging WCF Serialization issues

When serializing objects over WCF you might encounter the following not-so-helpful message.

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

And the Close method of your service proxy may complain that

System.ServiceModel.Security.MessageSecurityException : An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.

Which seems to indicate that there is a security exception. However, in my experience, the root problem has usually been a serialization issue.

To debug server-side issues, use a combination of the SvcTraceViewer.exe, an indispensible tool for debugging and tracing WCF messages, and simple tracing into the server-side code by attaching to the server process.

If indeed you have a serialization issue, you will be able to step through the Service method and the Server stack trace will end right at the point where you return from the service method.

Server stack trace:
   at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
...

However, at no point do you actually see the true source of the exception. It seems to get eaten somewhere in the channel. The best way that I have discovered to track down any serialization issues is to write unit tests that manually serialize the objects that pass through the WCF service. My serialization helper is a generic method like this:

        public static void LogDTO<TDTO>(TDTO dto)
        {
            DataContractSerializer ser =
                new DataContractSerializer(typeof(TDTO));
            FileStream writer = new FileStream(@"C:\log\"+dto.GetType().Name+".xml", FileMode.Create);
            ser.WriteObject(writer, dto);
            writer.Close();
        }
 

The unit test simply calls the LogDTO method for each serializable object.

Serializer.LogDTO<myDTO>(myDTOInstance);

And now you can resolve any serialization issues with an appropriate stack trace and exception message.

Posted: Mar 12 2007, 07:50 PM by admin | with 3 comment(s)
Filed under:

Comments

Asmodyne said:

Man, do I love this solution of yours !

This IS the perfect way to debug those horrible serialization issues encountered with WCF !

It helped me finding that a System.Drawing.Image datamembers could'nt be serialized just because it referenced a System.Drawing.Bitmap.

Dataserializer mechanisms cannot cope with classes inheritances ! *grin*

Again, thanks !

# August 10, 2007 3:03 AM

DDL said:

WCF e NHibernate

# October 21, 2008 8:50 AM

SunLover8 said:

Wow, that is a smooth and relatively simple solution. Much appreciated!

# November 10, 2009 4:41 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)