重用WCF服务客户端还是每次创建一个?

问题描述:

我听到(并阅读了)许多与以下哪个更好的意见有关的信息:重用WCF服务客户端或每次创建一个.但是我从未设法了解在这些情况下到底发生了什么.除了意见,我真的没有找到官方"的东西(例如msdn页面上说哪个更好,为什么和什么时候).

I heard (and read) a lot of opinions related to which is better: to reuse a WCF service client or create one each time. But I never managed to understand what exactly happens in those situations. And besides opinions, I really didn't find something 'official' (like a msdn page saying which is better, why and when).

在开发自己的应用程序时,我很少遇到每次创建(和关闭)一个问题的问题(创建和关闭过程并没有增加处理时间,甚至不花一毫秒的时间).但是在某些情况下,重用WCF服务客户端确实可以做出改变(一种很好的方式).这些是我在实践中观察到的,没有真正的逻辑动机.

In developping my own applications I rarely had problems with creating (and closing) one each time (the creating and closing proces didn't add in the processing time not even a milisecond). But in a few cases reusing a WCF service client really made a change (in a good way). These were observed by me in practice, without having a really logic motive.

那么,有人可以确切地解释我哪种方法更好吗? 在什么情况下?这取决于客户端负载吗?在IIS上使用Web Garden是否会影响选择?

So, can anyone explain me exactly which way is better? And in which situation? It depends on the client loading? Using Web Garden on IIS will affect the choice?

Microsoft建议使用缓存进行客户端创建: http://msdn.microsoft.com/zh-cn/library/aa738757.aspx (查看要考虑的问题"部分)

Microsoft recommends caching for client creation: http://msdn.microsoft.com/en-us/library/aa738757.aspx (check out the 'issues to consider' part)

在WCF的初始版本中,创建Channel Factory时存在性能问题,因此每次创建客户端都会导致性能问题.我们必须创建并缓存通道工厂. Microsoft通过更新(.NET Framework SP1?)修复了此问题,并且不再需要缓存.

In the initial version of WCF there was a performance problem creating Channel Factory, therefore creating a client each time cause performance problems. We had to create and cache the channel factory. Microsoft fixed this problem with an update (.NET Framework SP1?) and caching is not necessary anymore.

基本上,修复后的性能差异可以忽略不计,尤其是考虑到您正在通过网络拨打电话的情况下.

Basically, the performance difference, after the fix, is negligible, especially considering that you're making a call over network.