views:

28

answers:

2

I have converted my web service to wcf service which has some datacontracts. As a best practice it is mentioned and advisable that the DataContracts should inherit from IExtensibleDataObject. I get the point that in case of addition or removal of datamembers, IExtensibleDataObject is helpful. But i am not able to get how will the clients access removed datamembers. Here is my code:

[ServiceContract(Namespace = "http://mycompany.com/2010/08/")]
public class MyWebService {
    [OperationContract]
    public Employee Add(Employee emp)
    {
        // Some Processing
    }
}

[DataContract(Name = "Employee", Namespace = "http://mycompany.com/2010/08/")]
public class Employee : IExtensibleDataObject {
    [DataMember] public string FirstName;
    [DataMember] public string LastName;

    public ExtensionDataObject ExtensionData  { get; set; }
}

Now in my next version of web service I made some changes to DataContract as

[DataContract(Name = "Employee", Namespace = "http://mycompany.com/2010/09/")]
public class Employee : IExtensibleDataObject {
    [DataMember] public string FirstName;
    [DataMember] public string LastName;
    [DataMember(IsRequired = true)] public string MiddleName;

    public ExtensionDataObject ExtensionData  { get; set; }
}

However my client that is accessing my older version of web service is now getting error for not supplying the MiddleName field. I am still confused for the usage of IExtensionDataObject.

Please help.

A: 

Hello,

that is incorrect usage of IExtensibleDataObject. You have modified data contract on the server side an you have marked new field as required so it means you have broken versioning and nothing helps you.

IExtensibleDataObject is for other purpose. Let assume that you have modified your client so that data contract on the client contains MiddleName. Now you set the MiddleName and use Add service operation. What value of MiddleName will be in returned Employee object? If you don't use IExtensibleDataObject the value will be null, if you use IExtensibleDataObject the value will be same as you set to input parameter.

When using DataContractSerializer WCF throws away all non understood parameters. IExtensibleDataObject avoid this by storing all those parameters in special collection and sending them back to client.

If you want to use contract versioning forget about required fields. That is the first thing which will break it.

Best regards, Ladislav

Ladislav Mrnka
+1  A: 

Hi there, I'm afraid that's not the correct usage of IExtensibleDataObject, the IExtensibleDataObject interface is designed to support version round-tripping, have a read of this MSDN article on forward compatibility:

http://msdn.microsoft.com/en-us/library/ms731083.aspx

And here's another article on best practices on Data Contract versioning in general: http://msdn.microsoft.com/en-us/library/ms733832.aspx

theburningmonk