Anatomy of DataContractAttribute

This blog discusses data contract specification through DataContractAttribute.

Intent

To abstract Dot Net types such as value type, enum, array, classes, interfaces, collection type or generic as part of service contract message.

Noted that this is an approach to specify data contract. For more information, please see Making Sense With Data Contract (Structural Contract).

Remarks

  • DataContractAttribute is the simplest approach to specify data contract specification.
  • From design standpoint, not all Dot Net types are treated the same.  For more information, see Types Supported by the Data Contract Serializer and Serializable Types.
  • DataContractAttribute offers relatively faster runtime performance compared to other approaches.
  • DataContractAttribute adopts op-in approach. In other word, WCF maps Dot Net type members only if they are asked to explicitly.

Minimal Code to Specify Data Contract

Declare a Type as Data Contract

 1 namespace StructualContractLibrary
 2 {
 3   using System.Runtime.Serialization;
 4   using System.ServiceModel;
 5 
 6   /* Note 1 */ [DataContract]
 7   public class MyDataContract
 8   {
 9     /* Note 2 */ [DataMember] public string StringValue;
10 
11     /* Note 3 */ [DataMember] private int IntProperty { get; set; }
12 
13     /* Note 4 */
14     public bool BoolValue;
15     protected string StringProperty { get; set; }
16   }
17 
18   [ServiceContract]
19   public interface IMyService
20   {
21     [OperationContract]
22     void Operation(MyDataContract request);
23   }
24 }
  • Note 1. Specify DataContractAttribute to declare this class as data contract.
  • Note 2. Only class members with DataMemberAttribute are count as data contract declaration.
  • Note 3. Class accessibility does not restrict declaration. In this case a privater property is count as part of data contract.
  • Note 4. Members without DataMemberAttribute are skipped.

Declare Enum Type

 1 namespace StructualContractLibrary
 2 {
 3   using System.Runtime.Serialization;
 4   using System.ServiceModel;
 5 
 6   /* Note 1 */ [DataContract]
 7   public enum MyEnumDataContract
 8   {
 9     A,
10 
11     /* Note 2 */
12     [EnumMember] B,
13     [EnumMember] C
14   }
15 
16   [ServiceContract]
17   public interface IMyService
18   {
19     [OperationContract]
20     void Operation(MyEnumDataContract request);
21   }
22 }
  • Note 1. Specify DataContractAttribute to declare this enum as data contract.
  • Note 2. Enum value with EnumMemberAttribute is part of data contract. In this case, WCF includes value B and C but not A.

Additional Specification Options

Name and Namespace

 1 namespace StructualContractLibrary
 2 {
 3   using System.Collections.Generic;
 4   using System.Runtime.Serialization;
 5   using System.ServiceModel;
 6 
 7   /* Note 1 */ [DataContract(Name = "NamedContract",
 8                              Namespace = "http://wcf.kenwong.org/2011/10")]
 9   public class MyNamedContract
10   {
11     [DataMember(/* Note 2 */Name = "MemberA")]
12     private int IntValue { get; set; }
13 
14     [DataMember(/* Note 3 */Name = "MemberB")]
15     protected string[] StringArray;
16 
17     [DataMember]
18     protected MyList MyList;
19   }
20 
21   /* Note 4 */ [CollectionDataContract(Name = "List",
22                             ItemName = "Item",
23                             Namespace = "http://wcf.kenwong.org/2011/10")]
24   public class MyList : List<string> { }
25 }
  • Note 1. Specify Name value property replaces instance name.
  • Note 2. Specify Name value property replaces member name.
  • Note 3. Specify Name value property replaces member name, but not array item name.
  • Note 4. CollectionDataContractAttribute Name property replaces instance name. ItemName property replaces item name in the collection.

For more information, please see Data Contract Names and Collection Types in Data Contracts

Member Order

For more information, please see Data Member Order.

Default Member Value

For more information, please see Data Member Default Values.

Contract Version

For more information, please see

Dealing with Polymorphism

For more information, please see

Issues to Consider

The following topics is clarification of third tenet of SOA – Services Share Schema and Contract, Not Class. For more information, please see

API Reference

Further Study