More On System.Tuple And Serialization/Deserialization

3 commentsWritten on June 23rd, 2010 by
Categories: .NET bugs

As i mentioned in my last post, i ran into a serialization problem with a Tuple reference when returning it from a WCF call.  It failed with the typical exception you get when a type can not be deserialized. Unfortunately, the message of the exception that i got is the same as it is when a type can not be serialized, though both situations are actually quite different.  One of my readers left a comment saying that he didn’t have problems serializing/deserializing a Tuple (by the way: am i the only one who has major problems with typing the word ‘tuple’? whenever i wanna use it, i always end up with ‘type’ before i realize my mistake) through WCF. 

So why did it work for him, yet failed in my situation? The answer turns out to be quite simple.  If you look at the Tuple type of .NET 4 in Reflector, you’ll see this:

tuple_dotnet

So it actually is serializable.  The thing is, my client is not a .NET project, it’s Silverlight.  And the Tuple type in Silverlight looks like this:

tuple_silverlight

Silverlight still doesn’t support the Serializable attribute and the Tuple types don’t have a parameterless constructor, so that’s why it can’t deserialize a Tuple instance when it’s returned from a WCF call.  I can’t believe i didn’t think of this earlier since i’ve been bitten by this problem a few more times (not with the Tuple type, but with other Serializable classes) in the past, but then again, i can’t believe something like this still doesn’t work when using .NET 4, Silverlight 4 and WCF 4 together.