Tuesday, 9 February 2010

WCF Serialization Tips

Here are a few tips on how to improve WCF performance and traffic by simple putting the right attributes on your serializable classes:

1. "Avoid inferred data contracts (POCO). Always be explicit and apply the DataContract attribute" (C) Juval Löwy
2. "Use the DataMember attribute only on properties or read-only public members" (C) Juval Löwy
3. Mark [DataMember] only properties, that DO have to be serialized. Avoid marking calculated properties as DataMember
2. Consider using (IsReference = true) on classes to avoid data duplication and circular references.
3. Use short (one or two letters) Name on classes and properties to significantly reduce traffic. (C) Eyal Vardi
4. Specify short Value for EnumMember
5. Use parametrized DataContract property for generic classes
6. Use (EmitDefaultValue=false) on properties to reduce traffic


  public enum CustomerTypes
    [EnumMember(Value = "I")]
    [EnumMember(Value = "E")]
    [EnumMember(Value = "U")]

  [DataContract(IsReference = true, Name = "B{0}")]
  public class BusinessBase<T>
    T Id { get; set; }

  [DataContract(IsReference = true, Name = "C")]
  public class Customer : BusinessBase<int>
    [DataMember(Name="C", EmitDefaultValue=false)]
    public CustomerTypes CustomerType { get; set; }

    [DataMember(Name = "N", EmitDefaultValue = false)]
    public string Name { get; set; }

    [DataMember(Name = "P", EmitDefaultValue = false)]
    public Customer ParentCustomer { get; set; }

    public bool IsGoodCustomer
        return this.CustomerType == CustomerTypes.Internal || this.CustomerType == CustomerTypes.External;


* This source code was highlighted with Source Code Highlighter.
Post a Comment