WCF Data Contract and its Usages - A Comprehensive Guide for Beginners – C# and Vb.Net

← PrevNext →

In my previous article, I have explained about WCF Architecture and various Contracts such as Service Contract, Operation Contract and more.

Data Contract defines the structure of data, using which the client and service will exchange data. The WCF runtime uses a method called the Serializer to serialize and de-serialize the data on the network.

Data converted into an XML format before sharing on the network is serialization. Similarly, when the runtime extracts data from an XML file, it is de-serialization.

Versioning in WCF

Another important feature of WCF is its Versioning of Data Contract. It is like keeping a tab on changes made to a data type or a data member, after deploying the methods at the client and service level.

The versioning system allows us to manage changes to an existing data structure or data types.

We recommend you go through this MSDN link to understand best practice for versioning Data Contract.

Let us add a Data Contract to our project. We have created a project in our previous article where we have covered WCF Architecture and Service Contract. We will continue with the same project.

We will add a [DataContract] attribute and three [DataMember] attributes inside IBook.cs.

Note: You must declare the Data Contract and its members outside the public interface IBook.

Before adding the [DataContract] attribute, we first need to include the namespace System.Runtime.Serialization to our project. Now we are good to go.

Just add a new [OperationContract] attribute that will inherit the properties of the class declared inside the Data Contract.

C# Interface (IBook.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Runtime.Serialization;

[ServiceContract]
public interface IBook
{
    [OperationContract()]
    BookDetails ShowBookDetails(String sBookID);
}

[DataContract()]
public class BookDetails
{
    string sBookName;
    string sCategory;
    double dPrice;

    [DataMember()]
    public string Name
    {
        get { return sBookName; }
        set { sBookName = value; }
    }

    [DataMember()]
    public string Catagory
    {
        get { return sCategory; }
        set { sCategory = value; }
    }

    [DataMember()]
    public double Price
    {
        get { return dPrice; }
        set { dPrice = value; }
    }
}

We will add the newly defined function ShowBookDetails inside the Book class, since the class implements the IBook interface.

C# Class (Book.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Runtime.Serialization;

public class Book : IBook
{
    public BookDetails ShowBookDetails(string sBookID)
    {

        BookDetails objBookDetails = new BookDetails();

        if (sBookID == "001")
        {
            objBookDetails.Name = "Asp.Net 4 Blue Book";
            objBookDetails.Catagory = "Programming";
            objBookDetails.Price = 56;
        }

        return objBookDetails;
    }

}
Vb.Net Interface (IBook.vb)
Imports System.ServiceModel
Imports System.Runtime.Serialization

<ServiceContract()>
Public Interface IBook

    <OperationContract()>
    Function ShowBookDetails(ByVal sBookID As String) As BookDetails

End Interface

<DataContract()> _
Public Class BookDetails
    Dim sBookName As String
    Dim sCategory As String
    Dim dPrice As Double

    <DataMember()>
    Public Property Name() As String
        Get
            Return sBookName
        End Get
        Set(value As String)
            sBookName = value
        End Set
    End Property

    <DataMember()>
    Public Property Catagory() As String
        Get
            Return sCategory
        End Get
        Set(value As String)
            sCategory = value
        End Set
    End Property

    <DataMember()>
    Public Property Price() As Double
        Get
            Return dPrice
        End Get
        Set(value As Double)
            dPrice = value
        End Set
    End Property
End Class
Vb.Net Class (Book.vb)
Public Class Book
    Implements IBook

    Public Function ShowBookDetails(sBookID As String) As IBook.BookDetails 
        Implements IBook.ShowBookDetails

        Dim objBookDetails As New IBook.BookDetails

        If sBookID = "001" Then
            objBookDetails.Name = "Asp.Net 4 Blue Book"
            objBookDetails.Catagory = "Programming"
            objBookDetails.Price = 56
        End If

        Return objBookDetails
    End Function
End Class

Our Client Interface

Mark up

The client is our web page, which will have an input box to enter the Book’s ID and a button to submit. The DIV element will show the details of books returned by WCF service.

<div style="font:15px Arial;">
    <h2><strong>Calling my first WCF Service</strong></h2><br />

    <div>
        <b>Enter Book ID</b>:  <input id="txtID" type="text" runat="server" />
    </div>

    <div id="BooksDetails" runat="server" style="margin:10px 0;"> </div>

    <div style="margin:10px auto;">
        <input type="button" value="Submit" runat="server" onserverclick="Submit" />
    </div>
</div>
Code behind (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Submit(object sender, EventArgs e)
    {
        Book objBook = new Book();

        BooksDetails.InnerHtml = "Book: " + objBooks.ShowBookDetails(txtID.Value).Name + "<br />" + 
            "Category: " + objBooks.ShowBookDetails(txtID.Value).Catagory + "<br />" + 
            "Price: $" + objBooks.ShowBookDetails(txtID.Value).Price;
    }
}
Vb.Net
Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Submit(sender As Object, e As EventArgs)
        Dim objBook As New Book

        BooksDetails.InnerHtml = _
            "Book: " & objBooks.ShowBookDetails(txtID.Value).Name & "<br />" & _
            "Category: " & objBooks.ShowBookDetails(txtID.Value).Catagory & "<br />" & _
            "Price: $" & objBooks.ShowBookDetails(txtID.Value).Price
    End Sub
End Class
Conclusion

The entire process may look lengthy and tedious, however, with little coding you can catch up with WCF Contracts. Moreover, at the end, you will find this interesting and useful.

← PreviousNext →