WCF defaults stifles loose coupling

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:

 [ServiceBehavior(
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
}).ToArray();
}
public Segment[] Join()
{
return segments;
}
}
[ServiceContract]
public interface IDistributedHashTableMaster
{
[OperationContract]
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))
CreateChannel();
channel.Join();

Need to change to something like

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

References
0
Average: 5 (1 vote)

VP R&D of xsights (www.xsights.com) Arnon Rotem Gal-Oz has more than 19 years of experience managing, architecting and building large scale, mission critical, distributed systems. Before joining XSIGHTS, Arnon worked as the development manager of the Biometrics line in Rafael. Prior to that, he worked in various technical and managerial roles in large corporations including Microsoft, Amdocs and Matrix. Arnon is a DZone MVB and is not an employee of DZone and has posted 26 posts at DZone.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

bet replied on Mon, 2009/06/29 - 2:24pm

Lets start from beginning. I have started earn $$ with sports betting about 3 years ago. Firstly I was very very bed thinking about this kind of job. Lots of people said me that sports btting is just gambling and nothing more. That I have started read a lot of betting and bookmakers firms, bonuses ect. First what I found what very interested me was betfair and Betfair trading system it is nothing but exchange bets with other members of this system. It is amazing option earn lots of money. One more think what I love in betfair is great bonus with Sports betting. Firstly when You wanna start betting You have to choose of the betting system and next find good site with livescore. When You have more information about sport betting You could think about change Your bookmaker to something new like bwin or bet365. Last think what I wanna tell You is Good LUCK, read and learn as much as You can and finaly You got You succes with bookies.Best regards !!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.