Adsense

Thursday, June 14, 2007

Web Services, IDictionaries and Serialization... Oh My!

I have been building some .Net web services lately and created one that was trying to return a generic IDictionary. (IDictionary<int, int>) To my surprise and ultimate disappointment, I was presented with the following Server Error when trying to access my webservice...

"The type System.Collections.Generic.Dictionary ..<removed babble>.. is not supported because it implements IDictionary."

It looks like the XmlSerializer is unable to serialize objects that implement IDictionary. How annoying! I use a lot of generic dictionaries and it's a bit of a pain work around this XmlSerializer limitation. I was able to find a solution from this Aaron Skonnard post on the msdn site.

The solution shows you how to write a class that actually wraps your dictionary object. The values of your dictionary are copied to an array of serializable objects which are allowed to be passed through your web service and then deserialized on the other end. Pretty handy and worked really well in our solution.

Aaron warns that the IXmlSerializable interface is undocumented/unsupported feature of the .Net Framework and is subject to change at any time.

No comments: