0

I have data contract for WCF and its got custom class variable reference, I wondering do I need to do anything additional in configuration or anything else. i.e. AddressData

Member class

[DataContract]
public class MemberData : IIdentifiableEntity
{
    [DataMember]
    public int MemberID { get; set; }

    [DataMember]
    public string Title { get; set; }

    [DataMember]
    public string Surname { get; set; }

    [DataMember]
    public string Forename { get; set; }

    [DataMember]
    public string MiddleName { get; set; }

    [DataMember]
    public string PrevSurname { get; set; }

    [DataMember]
    public System.DateTime DOB { get; set; }

    [DataMember]
    public string Sex { get; set; }

    [DataMember]
    public AddressData Address { get; set; }

    [DataMember]
    public ContactDetailData ContactDetail { get; set; }

    [DataMember]
    public MembershipData Membership { get; set; }

    int IIdentifiableEntity.EntityId
    {
      get { return MemberID; }
      set { MemberID = value; }
    }
}

Address class

 public class AddressData
{
    public int MemberID { get; set; }

    public int AddressType { get; set; }

    public string AddressLine1 { get; set; }

    public string AddressLine2 { get; set; }

    public string AddressLine3 { get; set; }

    public string PostCode { get; set; }

    public string City { get; set; }

    public string County { get; set; }

    public string Town { get; set; }

    public string Country { get; set; }

    public MemberData Member { get; set; }
}
K.Z
  • 5,201
  • 25
  • 104
  • 240
  • How does your AddressData looks like? Basically what you pass to WCF should be serializable, if there are no Dictionaries, for example, inside it, then I think it should work as is. – 3615 Jun 17 '16 at 10:55
  • you have to declare the AddressData also with [DataContract] and all public fields with [DataMember]. Do you get any errors or what exactly is the question? – TryToSolveItSimple Jun 17 '16 at 11:01
  • No I got the answer and that is I have to also provide data contract for address class – K.Z Jun 17 '16 at 11:03
  • the fields you do not declare with [DataMember] are not serializable -> WCF uses serialisation for the communication / contract -> those fields will not be visible for the client (not in the contract). Just FYI. – TryToSolveItSimple Jun 17 '16 at 11:26
  • many thanks ... plus point to one of you question – K.Z Jun 17 '16 at 11:28
  • 2
    @TryToSolveItSimple this is not correct. WCF will serialize your service types regardless of whether you explicitly use DataMember attribute. http://stackoverflow.com/a/4836803/569662 – tom redfern Jun 17 '16 at 15:40

1 Answers1

2

do I need to do anything additional

While you don't need to use the DataMember attribute to decorate your service types (WCF will still serialize them, as long as they only contain serializable types), you should generally still use the attribute.

From https://stackoverflow.com/a/4836803/569662:

  • without [DataContract], you cannot define an XML namespace for your data to live in
  • without [DataMember], you cannot serialize non-public properties or fields
  • without [DataMember], you cannot define an order of serialization (Order=) and the DCS will serialize all properties alphabetically
  • without [DataMember], you cannot define a different name for your property (Name=)
  • without [DataMember], you cannot define things like IsRequired= or other useful attributes
  • without [DataMember], you cannot leave out certain public properties - all public properties will be serialized by the DCS
Community
  • 1
  • 1
tom redfern
  • 30,562
  • 14
  • 91
  • 126