Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a WCF service that receives too many parameters not breaking?

Tags:

c#

asp.net

wcf

OK this is a bit of a weird question. I have come across a section of code in a project which programmatically creates a ChannelFactory based on an interface. A method in the interface takes three parameters. Now when I looked at the actual WCF service's code, that method only expects two parameters but is being sent three from the client.

I would expect the service to break when receiving the extra parameter but it doesn't. Does anyone have any idea why this is working?

like image 298
AGH Avatar asked Dec 08 '25 03:12

AGH


2 Answers

The service call gets encoded onto the wire and sent to the receiver. Depending on how strict the XML/Binary/Json parsers are, the extra parameter is simply ignored.

When the stub code on the WCF server receives the wire call, it does not go over the serialized packet and says "They are calling MethodX and I got param1, param2 and param3 - let's try stuffing them inside the method... Oh. It only takes param1 and param2. Boom."

Instead, it says something like: "They called MethodX. Great. What parameters does it take? Param1 and Param2. Let's see if these are present in the packet. Oh! They are here. Sweet. I'll use them." It simply ignores the rest.

Some notes:

  1. It is not difficult to write a stub that will fail if extra stuff is there. WCF will fail on some extra stuff and not fail on others for example.
  2. There is a benefit to this where you can be forward and potentially backward compatible (i.e. Client V2 talking to Server V1 will still work, simply ignoring the parameter [which you may or may not want]).
like image 105
Shahar Prish Avatar answered Dec 10 '25 16:12

Shahar Prish


WCF has Forward Compatible Data Contracts.

If the service contract inherits from IExtensibleDataObject, then the service behavior is to store and not throw an error when extra input data is found.

When the WCF infrastructure encounters data that is not part of the original data contract, the data is stored in the property and preserved. It is not processed in any other way except for temporary storage. If the object is returned back to where it originated, the original (unknown) data is also returned. Therefore, the data has made a round trip to and from the originating endpoint without loss.

like image 33
ErnieL Avatar answered Dec 10 '25 15:12

ErnieL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!