entity framework - Solve a circular reference in WCF from my POCOs -
i have couple of classes (for now) , i'm trying clear circular reference between 2 since killing wcf's serialization.
i using ef pocos in wcf rest service helps. have simplified problem down bare bones 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 [datacontract(isreference = true)]
group class circular reference cleaned returned results end 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 properties of group , how can them?
britishdeveloper,
there no properties associated group. that's why see id of 1.
the reason annotate group class [datacontract(isreference = true)], telling datacontract serializer it's no longer poco type. it's datacontract type.
so, serialize group properties, need go ahead , annotate title , message properties datamemberattribute.
an alternative use "preserveobjectreferences", can pass parameter datacontractserializer, datacontractserializeroperationbehavior, , other classes.
hope helps!
Comments
Post a Comment