SIGN UP MEMBER LOGIN:    
ARTICLE

Versioning in WCF Data Contract: Part I - Adding New Members

Posted by Dhananjay Kumar Articles | WCF with C# October 04, 2010
DataContract versioning is required when a modification has been made to the existing DataContract exposed to the client from the service or vice versa. In this article we will explore New Member scenarios.
Reader Level:

DataContract versioning is required when a modification has been made to the existing DataContract exposed to the client from the service or vice versa. If either service or client changes the schema of DataContract exposed, then a new version of DataContract is needed and both parties involved in communication should be able to accommodate the changes done in the DataContract. 

The scenario that can cause for a new version of DataContract is as below, 
  1. Missing a member from existing DataContract.
  2. Adding a new member to existing DataContract.
  3. Round Tripping 
In this article we will explore New Member scenarios.

Adding a New Member to existing DataContract 

This is most common changes we do to give new version to DataContract. 

For example we have a DataContract through which we are exposing Product custom class to the client 

Product.cs

[DataContract]
    public class Product
    {
        [DataMember(Order = 1)]
        public string ProductNumber;
        [DataMember(Order = 2)]
        public string ProductName;
        [DataMember(Order = 3)]
        public string ProductPrice;
    }   

And we have ServiceContract which is returning a Product from the service 

IService1.cs

namespace WcfService10
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        Product GetaProduct();
    }
}

Service implementation is as below:

Service1.svc.cs

namespace WcfService10
{
    public class Service1 : IService1
    {
        public Product GetaProduct()
        {
            try
            {
                Product p = new Product { ProductName = "Pen", ProductPrice = "9", ProductNumber = "1" };
                return p;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}

Now at the client side, we can call the service as below. (Call this client as Client1)

Program.cs 

using ConsoleApplication1.ServiceReference1;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Service1Client proxy = new Service1Client())
            {
                var res = proxy.GetaProduct();
                Console.WriteLine(res.ProductName + res.ProductPrice + res.ProductNumber);
            }
            Console.ReadKey(true);
        }
    }
}

Output 

1.gif 

Now let us go ahead and modify the DataContract at the service. Just add one more parameter 

        [DataMember(Order = 4)]
        public string ProductColor; 

So now DataContract will look like 

Product.cs

[DataContract]
    public class Product
    {
        [DataMember(Order = 1)]
        public string ProductNumber;
        [DataMember(Order = 2)]
        public string ProductName;
        [DataMember(Order = 3)]
        public string ProductPrice;
        [DataMember(Order = 4)]
        public string ProductColor; 
    }

And now go ahead and modify the service implementation as below:

Service1.svc.cs

namespace WcfService10
{
    public class Service1 : IService1
    {
        public Product GetaProduct()
        {
            try
            {
                Product p = new Product { ProductName = "Pen", ProductPrice = "9", ProductNumber = "1",ProductColor="Red" };
                return p;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}

Now do not update the service reference at the clien1. What I mean here is that same version of DataContract at the client 1. And create one more client and call it as client 2. Use updated service at the client. 

So, now client 1 will look like exactly it was previously 

Program.cs (Client1)

using ConsoleApplication1.ServiceReference1;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Service1Client proxy = new Service1Client())
            {
                var res = proxy.GetaProduct();
                Console.WriteLine(res.ProductName + res.ProductPrice + res.ProductNumber);
            }
            Console.ReadKey(true);
        }
    }
}

Output would be exactly the same as it was with previous version of DataContract 

2.gif 

And new created client (Client2) will look like below, 

Program.cs(Client2)

using ConsoleApplication2.ServiceReference1;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Service1Client proxy = new Service1Client())
            {
                var res = proxy.GetaProduct();
                Console.WriteLine(res.ProductName + res.ProductPrice + res.ProductNumber+res.ProductColor);
            }
            Console.ReadKey(true);
        }
    }
}

This client is using version2 of DataContract and output would contain newly added member in DataContract. 

3.gif
 
So on deseralization new added DataMember will be ingnored by DataContractSeralizer and both party will continue working without breaking.

4.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor