Circular References During Serialization
Wow - here's a doozey for ya.
I've been playing tonight with my new "service oriented" version of Comicster's online database (as detailed here). Tonight's work has involved taking the existing assemblies and, with Visual Web Developer Express, wrapping them in web services.
The first one I tried, PublisherService, was really simple. It just returns an array of Publisher objects. No problem - the XML came back from the web service first time.
The second one, however, turned out to be a killer. TitleService.FindTitles returns an array of Title objects. That in itself isn't a problem. Each Title in that array has a property called Issues, which is itself an array of Issue objects. Still no problem. However, each Issue object has a property called Title, which (you guessed it) links back to the title the issue belongs to. That was the problem.
As soon as the web service tried to serialize the array of Title objects to XML, it threw an exception complaining about a circular reference. Smart! So now I had to work out (a) what was happening and (b) how to fix it.
As you know by now I worked out what was happening. How did I fix it? This might seem like a kludge, but bear with me.
What I did was not populate the Issue.Title property if the issue was being constructed from the TitleService class. So every issue that the TitleService created has Title == null.
The reason I don't consider this too bad a workaround is ... well ... the client already knows the title. The only way he can see that issue is by looking at the title's Issues property! So it's not like I'm losing data.
Leaving Issue.Title as null meant that the web service didn't have to include it in the XML when the class was serialized, so there was no circular reference any more. Problem? Problem solved!
ps. For what it's worth, I did actually try deriving Title from another type (TitleBase) and changing Issue.Title to the TitleBase type. Turns out that the .NET serializer is a bit smarter than that. Even though the property's type was TitleBase, it knew that the object it was pointing to was a fully-fledged Title and serialized it accordingly. Circular references again!

# Trackback from Jason Haley on 19/09/2006 12:36 PM