Versioning in WCF Data Contract Part #1:Adding New Members

No.of Views3482
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  06 Oct 2010 21:10:36
Tag : WCF , General
DataContract versioning requires when some modification has been done to the existing DataContract exposed to the client from the service or vice versa. If either service or client is changing the schema of DataContract exposed then new version of DataContract come into action and both party involved in communication should be able to accommodate the changes done in the DataContract.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

DataContract versioning requires when some modification has been done to the existing DataContract exposed to the client from the service or vice versa. If either service or client is changing the schema of DataContract exposed then new version of DataContract come into action and both party involved in communication should be able to accommodate the changes done in the DataContract.

The scenario causes for 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 

Image Loading

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

Image Loading

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 

Image Loading

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. 

Image Loading

Note:So on deseralization new added DataMember will be ingnored by DataContractSeralizer and both party will continue working without any breakage. 

Image Loading

That's all hopes this help.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top