WCF 3.5 SP1 allows for circular references without adding any code

April 7, 2009 by
Filed under: Programming, Software Configuration, WCF 

WCF now provides a way to avoid circular reference problems on serialization, which before required you to initialize the Data Contract Serializer with a KeepReferences setting. Another option was to cheat and use a NetDataContractSerializer which wasn’t easier without cusotm behaviors or special code, now it is as simple as adding an attribute.

 Here is one such exception you may receive if you are having issues:

“Object Graph for Type X Contains Cycles and

Cannot be Serialized if Reference Tracking is Disabled”

 

Example that will work wonders without anything else required.

[DataContract(IsReference = true)]

    public class Person

    {

        [DataMember]

        public string Name

        { get; set; }

       

        [DataMember]

        public string Address

        { get; set; }

 

  [DataMember]

        public List<Person> Children

        { get; set; }

 

    }

Now it works, Another example more appropriate is using eXtensible Access Control Markup Language (XACML)  specification.

[DataContract(IsReference = true)]

    public class PolicySet

    {

        [DataMember]

        public string Name

        { get; set; }

       

 

  [DataMember]

        public List<PolicySet> PolicySets

        { get; set; }

 

  [DataMember]

        public List<PolicySet> ParentPolicySet

        { get; set; }

 

 

    }

[DataContract(IsReference = true)]

    public class Policy

    {

[DataMember]

        public string Name

        { get; set; }

 

        [DataMember]

        public PolicySet ParentPolicySet

        { get; set; }

 

  [DataMember]

        public List<Rule> Rules

        { get; set; }

 

 

    }

[DataContract(IsReference = true)]

    public class Rule

    {

[DataMember]

        public string Name

        { get; set; }

 

        [DataMember]

        public Policy ParentPolicy

        { get; set; }

 

  [DataMember]

        public List<Targets> Targets

       { get; set; }

    }

Comments

One Comment on WCF 3.5 SP1 allows for circular references without adding any code

    Serializzazione: WCF perch…

    Serializzazione: WCF perch…

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!





%d bloggers like this: