Monotouch Deserialization FormatException “Input string was not in the correct format” – Solved

Ran into an interesting issue with some POCO entities being passed from my VS2010 WCF service to my Monotouch client. Here’s an example of how you cannot always trust the error message that is given to you.

I’m passing down an object graph, with a container object that contains an array of events and an array of People objects, each containing a Dictionary of event IDs and times. (Why nullable int as the key? Monotouch has issues with value types as dictionary keys, see Xamarin’s list of limitations.) The event IDs contained in there map to the events sent in the container object. Some of the people passed down may not have attended the event at all, meaning that aforementioned dictionary was empty. When I was getting the “Input string was not in the correct format” error, I thought that Monotouch WCF was bombing on a nullable int on the person being null, which didn’t make sense since I was pulling down the same person object in a different call that had nullable ints and everything worked peachy. Come to find out, it was the empty Dictionary causing the problem. In my service code, before passing the object back to the client, I loop through and null out any empty dictionaries in my object graph.

Hope this helps someone out there.