Over a million developers have joined DZone.

WCF defaults stifles loose coupling

DZone's Guide to

WCF defaults stifles loose coupling

· Web Dev Zone ·
Free Resource

Access over 20 APIs and mobile SDKs, up to 250k transactions free with no credit card required

This is another post (<Rant>) 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:

InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Single,
MaxItemsInObjectGraph = Int32.MaxValue
public class DistributedHashTableMaster : IDistributedHashTableMaster
private readonly Segment[] segments;
public DistributedHashTableMaster(NodeEndpoint endpoint)
segments = Enumerable.Range(0, 8192).Select(i =>
new Segment
AssignedEndpoint = endpoint,
Index = i
public Segment[] Join()
return segments;
public interface IDistributedHashTableMaster
Segment[] Join();
public class NodeEndpoint
public string Sync { get; set; }
public string Async { get; set; }
public class Segment
public Guid Version { get; set; }
public int Index { get; set; }
public NodeEndpoint AssignedEndpoint { get; set; }
public NodeEndpoint InProcessOfMovingToEndpoint { get; set; }
public int WcfHatesMeAndMakeMeSad { get; set; }


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

var channel =
new ChannelFactory<IDistributedHashTableMaster>(binding, new EndpointAddress(uri))

Need to change to something like

var channelFactory =
new ChannelFactory(binding, new EndpointAddress(uri));
foreach (var operationDescription in channelFactory.Endpoint.Contract.Operations)
var dataContractBehavior =
as DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
var channel=channelFactory.CreateChannel();
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…


#1 for location developers in quality, price and choice, switch to HERE.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}