How to Define a Data Contract for Custom Serialization in WCF?

For the WCF serializer to marshal a complex type, you must define a data contract for it. To do this, start by applying the DataContractAttribute attribute to the complex type definition that you want to pass in a message. Apply the DataMemberAttribute attribute to each field (either a data member or a property) that is to be serialized. All other data fields on the class are ignored.

By default, missing data members are ignored when deserializing data and their equivalent data members are set to the default value for that type. For example, the default setting for an integer field is zero. Use the IsRequired property of DataMemberAttribute to ensure that the specified data field is provided in the message. For serialization purposes, language-level visibility is irrelevant.

You should only mark the subset of data that you need to pass with the DataMemberAttribute attribute. You should avoid any unnecessary properties that are difficult to serialize.

If you are serializing a property rather than a data member, the property must have a set accessor and a get accessor.

Defining a Data Contract for Custom Serialization

If you are serializing a property rather than a data member, the property must have a set accessor and a get accessor. The following code examples show how to serialize a property

[Visual Basic]

<DataContract()> _

Public Class TransferRecord

    <DataMember()> _

    Public targetAccount As String


    <DataMember()> _

    Private transferringBank As String


    Private internalCustomerId As String


    Private m_transferId As String


    <DataMember()> _

    Public Property TransferId() As String


            Return m_transferId

        End Get


            m_transferId = value

        End Set

    End Property

End Class


[Visual C#]


public class TransferRecord



    public string targetAccount;



    private string transferringBank;


    private string internalCustomerId;


    private string transferId;



    public string TransferId


        get { return transferId; }

        set { transferId = value; }




Note that, in the preceding class, the value of transferId is still passed across to the other side because it is exposed by the TransferId property.

The following example shows how to create a data contract for the Person type by applying the DataContractAttribute and DataMemberAttribute attributes to the class and its members

using System;

using System.Runtime.Serialization;



public class Person


    // This member is serialized.


    internal string FullName;


    // This is serialized even though it is private.


    private int Age;


    // This is not serialized because the DataMemberAttribute

    // has not been applied.

    private string MailingAddress;


    // This is not serialized, but the property is.

    private string telephoneNumberValue;



    public string TelephoneNumber


        get { return telephoneNumberValue; }

        set { telephoneNumberValue = value; }




Ways to Consume Data Contracts

Client applications require access to the data contract to send messages that have the appropriate structure to the service. The most appropriate way that a client application can obtain information about a data contract depends on which platform or technology the client application is using, and the relationship between the client application and the service development.

In most cases, when you build a client application, you will generate the data types that are defined by the data contract from the service metadata. Visual Studio provides Svcutil.exe for this purpose, in addition to the Add Service Reference feature.

However, if the same team or closely related teams are developing the client application and the service, you can share the types between the client application and the service by storing them in a common library and referencing this library directly from the client and service applications

Here is a Walkthrough on How to Create and Consume WCF Services.

Walkthrough: Creating and Accessing WCF Services



1.       How to: Create a Basic Data Contract for a Class or Structure

2.      Walkthrough: Creating and Accessing WCF Services