Reputation: 3305
Let's assume I have a webservice which returns a class on all methods, informing the client the status of the process, for example:
public class WsResult {
string result; // either "error" or "ok"
}
Now we'd like to add a property to this class, without forcing all clients consuming our service to update their software. Is this possible?
For example:
public class WsResult {
public string result; // either "error" or "ok"
public Guid? someIdentifier;
}
I'm looking for answers on both WCF and ASMX.
Upvotes: 10
Views: 148
Reputation: 675
With WCF you can actually have different classes on the client and server side, which is what will normally happen as well when using the Add service reference UI. The trick to make this work is to set the namespace and the name on the DataContract
attribute (and perhaps even the name on the DataMember
attribute if the member names differ).
Only the properties that match will be deserialized, as such adding a property on the server side won't impact the client side.
Upvotes: -1
Reputation: 6238
WCF
It is possible. You can simply add a new property and it'll work providing that this new property is not required. For more details see point 8 of Best Practices: Data Contract Versioning article.
If you need to handle round-tripping scenario you should read about IExtensibleDataObject
interface. Round-tripping occurs when data is sent from a server to a client and it is expected that it'll be sent back. See Forward-Compatible Data Contracts article for details.
ASMX
With ASMX a situation is the same. You can add a new property and all clients should work. In this case you can also use IExtensibleDataObject
interface.
Final Comments
This answer is based on empirical tests with VS 2015. I strongly suggest you to do the same i.e.: write simple WCF/ASMX servers and clients and verify behaviour described by me. It took me just several minutes to do so. Or even better you can use already existing services.
I recommend additional tests because you may be using some non-default configuration which changes the default behaviour of WCF/ASMX services so it is better to check. I'm not aware of this kind of configuration but you never know.
Upvotes: 5