WCF ConcurrencyMode.Multiple Connection best practices and Caching

做~自己de王妃 提交于 2020-01-14 04:57:50

问题


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?

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1609703/wcf-concurrencymode-multiple-connection-best-practices-and-caching

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!