cadrell0
cadrell0

Reputation: 17307

Silverlight serialize object with cycles in object graph

I'm having an issue serializing objects when sending them to my WCF services. My classes look like this.

public class Foo
{
     public Bar Bar { get; set; }
}

public class Bar
{
    public Foo Bar { get; set; }
}

This causes a cycle in my object graph. I've fixed this on the server end by using the PreserveReferencesOperationBehavior. However, I still get an error when I try to serialize the objects in Silverlight.

While I can mark my objects with [DataContract(IsReference = true)], I'd prefer not to use this method because I have a large number of classes, many of which have over 100 properties and I don't want to have to add the [DataMember] attribute to each property.

Is there any other way to tell Silverlight to preserve references?

If it matters at all, I am using EntityFramework 4 with Code First.

Upvotes: 2

Views: 246

Answers (1)

AnthonyWJones
AnthonyWJones

Reputation: 189457

The infered DataContract behaviour of the serializer is present to assist in the simple DTO scenarios. If you want to do it "properly" you should be using the DataContract and DataMember attributes.

When you find you have anything other than the most simple of scenarios you just need to do things properly. The correct and only way to handle circular references is with IsReference.

Lesson here is that helpful magic pixie dust only goes so far after that you just need to put in the graft. Sorry its not the answer you were looking for.

Upvotes: 3

Related Questions