0

I have a couple of classes (for now) and I'm trying to clear up a circular reference between the two since it is killing WCF's serialization.

I am using EF with POCOs in a WCF REST service is that helps. I have simplified my problem down to bare bones for an easy example here:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Groups
{
    [WebGet(UriTemplate = "")]
    public Message GetCollection()
    {
        var message = new Message { Body = "Test message" };
        var group = new Group { Title = "Title of group" };
        message.Group = group;
        group.Messages = new List<Message> { message };
        return message;
    }
}

public class Message
{
    public string Body { get; set; }
    public Group Group { get; set; }
}

[DataContract(IsReference = true)]
public class Group
{
    public string Title { get; set; }
    public ICollection<Message> Messages { get; set; }
}

I have added the [DataContract(IsReference = true)] to the Group class so that the circular reference is cleaned up however my returned results end up like this:

<Message xmlns="http://schemas.datacontract.org/2004/07/LmApi" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Body>Test message</Body>
    <Group z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"/>
</Message>

Where are the properties of the Group and how can I get them?

BritishDeveloper
  • 13,219
  • 9
  • 52
  • 62

3 Answers3

0

This is similar to Circular References and WCF Here is my answer modified for this case

I had the same problem and resolved it by excluding the navigation property back to the parent from the DataContract

[DataContract]
public partial class Message
{

        [DataMember]
        public virtual string Body { get; set; }

        [DataMember]
        public virtual Group Group { get; set; }

}

[DataContract]
public partial class Group
{
        [DataMember]
        public virtual string Title { get; set; }

        public virtual ICollection<Message> Messages {get; set;}
}
Community
  • 1
  • 1
Nigel Findlater
  • 1,684
  • 14
  • 34
0

BritishDeveloper,

There are no properties associated with Group. That's why all you see is the ID of 1.

The reason is that as soon as you annotate the Group class with [DataContract(IsReference = true)], you are telling the DataContract serializer that it's no longer a POCO type. It's a DataContract type.

So, to serialize Group with properties, you now need to go ahead and annotate the Title and Message properties with DataMemberAttribute.

An alternative would be use the "preserveObjectReferences", which you can pass as a parameter to DataContractSerializer, DataContractSerializerOperationBehavior, and other classes.

Hope this helps!

krisragh MSFT
  • 1,908
  • 1
  • 13
  • 22
0

I decided to make my own smaller classes that have a constructor that takes an entity and sets all of this lighterweight properties correctly.

Basically it is a very small copy of the class that has just the properties needed in the payload. (Obviously I have excluded the problem navigation properties)

BritishDeveloper
  • 13,219
  • 9
  • 52
  • 62