views:

990

answers:

2

I've got a WCF service with the following settings:

  • NetNamedPipeBinding
  • ConcurrencyMode.Multiple
  • InstanceContextMode.Single

Now I've got a client that accesses to this WCF service in a multi-threaded fashion.

As far as I understand I have to open a new connection to the service for each thread to avoid threads to block each others.

  • In this case how expensive is the Open() call (service is in the same computer)?
  • Shall I cache/pool the my client class? (derived from ClientBase) or does WCF provides a transparent pool for connections similar to SQLConnection Pooling?
+2  A: 

WCF unfortunately does not pool client connections. I've found that Open() is relatively slow and have built my own pooling mechanisms keeping a handful of persistent connections open between the client and server.

One common gotcha though regarding this is that if even something as simple as a time-out occurs between the client and server (or any sort of CommunicationException is thrown), the client instance enters a Faulted state and becomes unusuable. At which time you must destroy and replace it w/ a new instance.

James Alexander
aghh! that's pretty rubbish. I actually already built a caching system but I just don't like the idea of extra complexity. Thanks for the clarification +1.
dr. evil
I doubt the Open() call is slow in a "on-machine-only" call scenario with the NetNamedPipe binding. Yes, it might be slow using wsHttpBinding (the default) which has lots of WS-* feature overhead - but named pipes are very efficient, and on-machine only, next to no security - that should be no problem at all.
marc_s
Even w/ NetNamedPipe, there's more overhead than I could stomach. Give it a whirl, seriously. On my lappy I saw the instantion time range from 150ms to 350ms. I ended up putting a pub/sub in front of a dispatcher w/ a pool of clients and saw pretty spectacular performance.
James Alexander
+1  A: 

James Alexander's answer is spot on (you have to pool connections yourself), but I figured I'd post a link to a blog entry that discusses an implementation that adds connection pooling on top of ClientBase. Here's the follow up post where he goes into details and provides a link to download the code.

Drew Marsh
good stuff, especially benchmarking. How about some code ? :) I already added some caching but your implementation looks neat, it would be nice to take a sneak peek :)
dr. evil
The blog post is actually not mine (don't want to steal credit!). I just know about it because I've researched this subject before. I should have given you the other link to his post where he actually gives more details and code. I will edit my entry.
Drew Marsh
ops! I see I thought that was your blog, anyway definitely good link to prove the point.
dr. evil