Question

If I need to go from this service contract:

[ServiceContract(Namespace="http://api.x.com/Svc1")]
public interface IService1
{
   [OperationContract(Name = "AddCustomer")]
   bool AddCustomer(DTOCustomer1 customer);
}

to this:

[ServiceContract(Namespace="http://api.x.com/Svc1")]
public interface IService1
{
   [OperationContract(Name = "AddCustomer")]
   bool AddCustomer(DTOCustomer2 customer);
}

and according to this good article: Versioning WCF I understand that when data contract is changed there is a need of defining a new vs of data contract in new namespace followed by defining a new vs of service contract in new namespace, after which a new endpoint should be added.

How exactly am I suppose to have this done. Is there an example anywhere? Could you write something based on my service contract shown above?

Thank you in advance!

Was it helpful?

Solution

According to the linked article you should do something like:

[ServiceContract(Namespace="http://api.x.com/Svc1")]
public interface IServiceNew : IService1
{
   [OperationContract(Name = "AddCustomerNew")]
   bool AddCustomer(DTOCustomer2 customer);
}

Then implement it in your service:

public class MyCurrentServiceImplementation : IServiceNew 
{...}

You will need to redeploy your service but existing clients should be able to continue to call the AddCustomer operation, and new clients can call the AddCustomerNew operation.

OTHER TIPS

It's very important to note that the assumption you state in your post:

"when data contract is changed there is a need of defining a new vs of data contract in new namespace"

is not always true. See "Data Contract Versioning" on MSDN for a number of cases where a data contract change is non-breaking and may therefore require no action other than perhaps modifying the internal implementation of your service method to handle the presence/absence of certain data due to differences between data contract versions.

In this specific example I would question how two versions of a method called AddCustomer can vary so much in their intent that it justifies creating a new service interface. Without seeing your old and new data contracts I can't know for sure, but I'm guessing that the real issue here is that the method has evolved to accept additional customer information.

If that's true, then it's very much like the situation of optional arguments in a method call. WCF is designed to handle this scenario very nicely as a non-breaking change to the data contract. As long as you can follow the guidelines in "Best Practices: Data Contract Versioning" on MSDN, then calls supplying either the old or new version of the contract will be accepted just fine by your existing service interface. Your service method will get the data that is possible given the combination of the client and server data contracts.

I would keep my service interface coherent, simple, and clean (i.e. avoid doing things like IServiceNew) and instead just add to the data contract and modify the implementation of AddCustomer to adapt to the whatever data it receives.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top