Defining a WCF Data Contract

A data contract defines the data that will be passed between a client and a service. Different attributes can be used to define a data contract such as DataContract and DataMember. The DataContract attribute applies to a class, whereas the DataMember attribute applies to a field or property. Public properties are recommended over public fields especially when you'll be data-binding classes to Silverlight controls.

An example of creating a simple contract to allow a Product object to be exchanged between a service and a client is shown next:

namespace Model {

[DataContract]

public partial class Product {

[DataMember]

public int ProductID { get; set; } [DataMember]

public int CategorylD { get; set; } [DataMember]

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

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

public string ProductImage { get; set; } [DataMember(Order=6)]

public decimal UnitCost { set; get; } [DataMember]

public string Description { get; set; }

Although you can always type this class and its members yourself, LINQ to SQL provides a nice designer surface that can be used to create data contract classes visually and tie them to a database table for simplified O/R mapping. The sample code provided for this chapter uses LINQ to SQL. If you go this route, you'll need to ensure that you change the Serialization Mode to Unidirectional in the LINQ to SQL designer so that the generated classes can be serialized and deserialized when used with WCF services. You can change the Serialization Mode value by right-clicking on the LINQ to SQL design surface and selecting Properties from the menu.

Data entity classes used in services can also be generated using XML schemas (.xsd files) and command-line tools. By doing this, messages exchanged between the client and service will be based on global standards that help to minimize interop issues across different platforms. With .NET you can use the xsd.exe tool with the /classes switch to generate classes from an XSD schema:

xsd.exe /classes schemaName.xsd

While this will generate the appropriate class and member properties for you, it won't decorate the class with the DataContract attribute and the properties with the DataMember attribute, unfortunately.

WCF's svcutil.exe tool can be used to convert an XSD schema into a class and add the appropriate DataContract and DataMember attributes for you. For example, to automatically generate a data contract class from an existing XSD schema, the following can be run using the Visual Studio command prompt:

svcutil.exe /dconly schemaName.xsd

The /dconly switch says to create the data contract class from the types defined in the schema. Running this command-line tool will auto-generate a class based on the schema types.

+1 0

Average user rating: 5 stars out of 1 votes

Responses

  • ramadevi1227@gmail.com
    • RATE
    what exactly wcf contains?what is WCF ABC?
    10 months ago

Post a comment

  • Receive news updates via email from this site