WCF - Manual Versioning

If I need to switch from this service contract:

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

:

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

and in accordance with this good article: WindowsF Versions I understand that if you change the data contract, you must define a new vs data contract in the new namespace, followed by the definition of a new service contract in the new namespace, and then add a new endpoint.

How exactly do I assume this is done. Is there any example? Could you write something based on my service contract shown above?

Thank you in advance!

+4
source share
2 answers

According to the related 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 execute it in your service:

 public class MyCurrentServiceImplementation : IServiceNew {...} 

You will need to redeploy your service, but existing customers must be able to continue with the AddCustomer operation, and new customers may invoke the AddCustomerNew operation.

+4
source

It is very important to note that the assumption that you indicate in your post:

"when the data contract changes, you must define a new vs of data in the new namespace"

not always true. See “Contract data versions” on MSDN for a number of cases where changing the data contract is not interrupted and therefore may not require any action, except possibly changing the internal implementation of your service method to handle the presence / absence of certain data due to differences between versions of the data contract.

In this particular example, I would question how the two versions of the method called AddCustomer can differ greatly in their intention, which justifies the creation of a new service interface. Without seeing your old and new data contracts, I cannot know for sure, but I assume that the real problem is that this method has evolved to accept additional information about the client.

If true, then this is very similar to the situation with optional arguments in a method call. WCF is designed to handle this scenario very nicely, like an inextricable change to a data contract. As long as you can follow the recommendations in "Best Practices: Data Versioning" on MSDN, then calls that deliver either the old or the new version of the Contract will be fully approved by your existing service interface. Your service method will receive the data that is possible, given the combination of client and server data contracts.

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

+3
source

Source: https://habr.com/ru/post/1390280/


All Articles