WCF defaults stifles loose coupling
<Rant>
This is another post about WCF default behavior and how it can make the life of developers miserable ( you can also check out “WCF defaults limit scalability” and “Another WCF gotcha - calling another service/resource within a call”)
Anyway, the trigger for this is a post by Ayende called “WCF works in mysterious ways”. Ayende posted some code he wrote which was throwing a serialization exception. You can see his post for the full code, but in a nut shell he was defining a large object graph (8192 objects that contain other objects) and was trying to send that over the wire. Here’s a short excerpt from the service definition:
<span style="color: #606060"> 1:</span> [ServiceBehavior(
<span style="color: #606060"> 2:</span> InstanceContextMode = InstanceContextMode.Single,
<span style="color: #606060"> 3:</span> ConcurrencyMode = ConcurrencyMode.Single,
<span style="color: #606060"> 4:</span> MaxItemsInObjectGraph = Int32.MaxValue
<span style="color: #606060"> 5:</span> )]
<span style="color: #606060"> 6:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DistributedHashTableMaster : IDistributedHashTableMaster
<span style="color: #606060"> 7:</span> {<span style="color: #606060"> 8:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> Segment[] segments;
<span style="color: #606060"> 9:</span>
<span style="color: #606060"> 10:</span> <span style="color: #0000ff">public</span> DistributedHashTableMaster(NodeEndpoint endpoint)
<span style="color: #606060"> 11:</span> {<span style="color: #606060"> 12:</span> segments = Enumerable.Range(0, 8192).Select(i =>
<span style="color: #606060"> 13:</span> <span style="color: #0000ff">new</span> Segment
<span style="color: #606060"> 14:</span> {<span style="color: #606060"> 15:</span> AssignedEndpoint = endpoint,
<span style="color: #606060"> 16:</span> Index = i
<span style="color: #606060"> 17:</span> }).ToArray();
<span style="color: #606060"> 18:</span> }
<span style="color: #606060"> 19:</span>
<span style="color: #606060"> 20:</span> <span style="color: #0000ff">public</span> Segment[] Join()
<span style="color: #606060"> 21:</span> {<span style="color: #606060"> 22:</span> <span style="color: #0000ff">return</span> segments;
<span style="color: #606060"> 23:</span> }
<span style="color: #606060"> 24:</span> }
<span style="color: #606060"> 25:</span>
<span style="color: #606060"> 26:</span> [ServiceContract]
<span style="color: #606060"> 27:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IDistributedHashTableMaster
<span style="color: #606060"> 28:</span> {<span style="color: #606060"> 29:</span> [OperationContract]
<span style="color: #606060"> 30:</span> Segment[] Join();
<span style="color: #606060"> 31:</span> }
<span style="color: #606060"> 32:</span>
<span style="color: #606060"> 33:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NodeEndpoint
<span style="color: #606060"> 34:</span> {<span style="color: #606060"> 35:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Sync { get; set; }<span style="color: #606060"> 36:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Async { get; set; }<span style="color: #606060"> 37:</span> }
<span style="color: #606060"> 38:</span>
<span style="color: #606060"> 39:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Segment
<span style="color: #606060"> 40:</span> {<span style="color: #606060"> 41:</span> <span style="color: #0000ff">public</span> Guid Version { get; set; }<span style="color: #606060"> 42:</span>
<span style="color: #606060"> 43:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Index { get; set; }<span style="color: #606060"> 44:</span> <span style="color: #0000ff">public</span> NodeEndpoint AssignedEndpoint { get; set; }<span style="color: #606060"> 45:</span> <span style="color: #0000ff">public</span> NodeEndpoint InProcessOfMovingToEndpoint { get; set; }<span style="color: #606060"> 46:</span>
<span style="color: #606060"> 47:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> WcfHatesMeAndMakeMeSad { get; set; }<span style="color: #606060"> 48:</span> }
As you can see in line 4 – the service is properly decorated with an attribute to enlarge the number of objects in graph. so looking at the code I initially suggested he add a few ServiceKnowType and DataContract/DataMember attributes on the data classes (as the serialization sometimes needs some guidance. After that didn’t help I actually ran the code and then I noticed that the code was missing setting that same attribute – on the client side. So to fix the problem, the client side code below
<span style="color: #606060"> 1:</span> var channel =
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">new</span> ChannelFactory<IDistributedHashTableMaster>(binding, <span style="color: #0000ff">new</span> EndpointAddress(uri))
<span style="color: #606060"> 3:</span> .CreateChannel();
<span style="color: #606060"> 4:</span> channel.Join();
<span style="color: #606060"> 1:</span> var channelFactory =
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">new</span> ChannelFactory(binding, <span style="color: #0000ff">new</span> EndpointAddress(uri));
<span style="color: #606060"> 3:</span>
<span style="color: #606060"> 4:</span> <span style="color: #0000ff">foreach</span> (var operationDescription <span style="color: #0000ff">in</span> channelFactory.Endpoint.Contract.Operations)
<span style="color: #606060"> 5:</span> {<span style="color: #606060"> 6:</span>
<span style="color: #606060"> 7:</span> var dataContractBehavior =
<span style="color: #606060"> 8:</span>
<span style="color: #606060"> 9:</span> operationDescription.Behaviors[<span style="color: #0000ff">typeof</span>(DataContractSerializerOperationBehavior)]
<span style="color: #606060"> 10:</span>
<span style="color: #606060"> 11:</span> <span style="color: #0000ff">as</span> DataContractSerializerOperationBehavior;
<span style="color: #606060"> 12:</span>
<span style="color: #606060"> 13:</span> <span style="color: #0000ff">if</span> (dataContractBehavior != <span style="color: #0000ff">null</span>)
<span style="color: #606060"> 14:</span> {<span style="color: #606060"> 15:</span>
<span style="color: #606060"> 16:</span> dataContractBehavior.MaxItemsInObjectGraph = <span style="color: #0000ff">int</span>.MaxValue;
<span style="color: #606060"> 17:</span>
<span style="color: #606060"> 18:</span> }
<span style="color: #606060"> 19:</span>
<span style="color: #606060"> 20:</span> }
<span style="color: #606060"> 21:</span> var channel=channelFactory.CreateChannel();
<span style="color: #606060"> 22:</span> channel.Join();
The main problem I find with this piece of code is the fact that it is needed at all. As the post’s title suggest I find this behavior greatly affects the loose coupling of anything that uses WCF (services or other components).
WCF requires that any change you make to the channel on the server side would be reflected in the channel on each and every client (e.g. we have a similar setting where we enlarge message sizes for webHttpBinding and there are many other such examples).
Sure, you say, that is just like adding a new field in the contract isn’t it? – Well no it isn’t since unlike anything else which appears in the (verbose as it is) SOAP contract these changes in default values, which are purely a WCF design choice, are not documented. Again, the changes in default values are not part of the contract. These are things you need to remember to pass on to you service consumer. So not only do I pay the overhead of having an explicit contract (e.g. vs. REST) – it really doesn’t work. It means that two components who use the same contract may not be interchangeable if one returns more data (in this case). It means that the two sides are coupled by the need to change these defaults and for what? WCF is smart enough to know how long is the message; WCF is smart enough to handle the message (if I encourage it by setting a behavior) why can’t it add 2 and 2 by itself?
Sometimes I just wish WCF had a TrainingWheels or DemosOnly attribute I could just set to false and make all this crap go away…
</Rant>

