[DataContract] Attribute in WCF

Data contracts are defined declaratively just like service contracts and operation contracts. To define a data contract, you simply decorate a class or enumeration with the [DataContract] attribute.

[DataContract]
public class BuyStock
{
    ...
}

I should back up and say that there is one difference between defining a service contract and a data contract. With service contracts, you can annotate a class or interface. Not so with data contracts. Data contracts can be defined by annotating a class, enumeration, or even a structure, but not an interface.
The preceding example defines a data contract. Once you have the data contract defined, you can define the members of a data contract. These members are the fields or properties of your class. Taking the previous example, the following example defines some data members for within the data contract:

[DataContract]
public class BuyStock
{
    [DataMember] public string symbol;
    [DataMember] public int quantity;
    [DataMember] public decimal price;
}

Once the data contract and its members are defined, you can choose to include that information in a service contract as follows:

[ServiceContract]
public interface IBuyStock
{
    [OperationContract]
    Void BuySomeStock(BuyStock stock);
}

Each data contract must have a unique name, but as a default these values are pulled from the class. It is important to also note that each member of the contract must be explicitly defined using the [DataMember] attribute. This helps to ensure that the developer meant to expose the data.

Name

The Name property is used to get or set the name of the data contract. This property is used as the name of the type in the XML schema. If this property is left blank, the default name is taken from the class in which the attribute is applied.
In the following example, the Name property sets the name to StockPurchase:

[DataContract(Name=”StockPurchase”]
public class BuyStock
{
    ...
}

Usually the default name is sufficient, but there are a couple of reasons why changing the name would be beneficial:

  • To allow the creation of XML element names that are considered invalid as type names. For example, “%Name” is an invalid type name but is a valid value for this property.
  • There might be a mismatch between the data and the associated contract. For example, the data contract is expecting, or requires, the element name to be “CompanyName,” but the typed name is actually “Name.” In this case the contract would fail because of the discrepancy. However, using the Name property solves this problem by letting you define or provide a name for the contract, so in this example you would set the property to a value of “CompanyName.”

Namespace

The Namespace parameter, a URI, specifies the namespace for the data contract. The default value for this parameter comes from the CLR namespace. The following example specifies a namespace to identify the data contract:

[DataContract(Namespace="http://www.PlanetofCoders.com")]
public class BookOrder
{
    ...
}
Tagged . Bookmark the permalink.

Leave a Reply