Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a new parameter to a WCF operation: choices?

What's the best way to handle adding a new (optional) parameter to an existing operation without requiring the client to update their WSDL? I do not want to update the namespace to describe a new version of the service contracts, since this should be backwards compatible with older clients.

Should I add a new operation with a new parameter, as an overload? Or should I just add the parameter to the existing operation?

Here is my operation:

[OperationContract]
MyResponse GetData();

Should it be:

[OperationContract]
MyResponse GetData(); 

[OperationContract]
MyResponse GetData(string filter);

Or more simply, just change it to this:

[OperationContract]
MyResponse GetData(string filter);

The latter option seems best, and according to my reference book, "The impact on client is none. New parameters are initialized to default values at the service." Is WCF initializing it to the the so-called default value? If so, what is the default value?

like image 733
Mike Atlas Avatar asked Nov 17 '09 21:11

Mike Atlas


3 Answers

One thing to take into consideration is that you can't have two OperationContracts with the same name. The way it's serialized it will throw an error.

The best approach is to go with Option 3 (just adding the new parameter) and within the method logic account for it being a null value for those clients that haven't updated yet. If it's a breaking change that the clients will need to update for, make sure to not have the entire application die because of the exception.

like image 95
Agent_9191 Avatar answered Oct 07 '22 10:10

Agent_9191


Well, changing an existing contract after it's been used is really against all rules of service orientation; you should never ever break an existing contract.

In reality, this happens quite frequently, and WCF is pretty good about handling that for you. As long as you only introduce non-breaking changes, existing clients will continue to work.

This can be:

  • a new operation contract on an existing service contract
  • a new non-required field on a DataContract

What you're trying to do is not going to work, though

  1. you cannot have two method with the same name in WCF - WCF is not .NET and you cannot have two methods by the same name being different only by their signature. Doesn't work. You'll need to use two separate, distinct names. Remember: your WCF method calls will be translated into a WSDL (web service description language) document to describe the service - and WSDL simply does not support having two operations with the same name - just a difference in signature is not supported and will not work.

  2. you cannot change the existing contract, e.g. you cannot introduce a new parameter into a method call after the fact, without breaking the contract.

So what you really need to do is this:

[OperationContract]
MyResponse GetData(); 

[OperationContract]
MyResponse GetFilteredData(string filter);

Any other change you suggested will a) break the contract, or b) simply not work in WCF:

like image 29
marc_s Avatar answered Oct 07 '22 12:10

marc_s


you can try this:

[OperationContract]
MyResponse GetData(); 

[OperationContract(Name = "GetDataByFilter")]
MyResponse GetData(string filter);
like image 40
sami Avatar answered Oct 07 '22 11:10

sami