Channels ▼

Arnon Rotem-Gal-Oz

Dr. Dobb's Bloggers

WCF defaults stifles loose coupling

June 07, 2009

<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();
Need to change to something like
<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>

 

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video