为什么缓存 WCF 通道是一件坏事?

发布于 2024-11-10 11:08:30 字数 783 浏览 3 评论 0 原文

我在网上阅读了很多 WCF 文章,似乎大多数人都会缓存 ChannelFactory 对象,但不会缓存通道本身。似乎大多数人都害怕使用通道缓存,因为他们不想处理可能导致缓存通道无法使用的网络故障。但是,通过捕获方法上的 CommunicationException、重新创建通道并使用反射重播该方法,可以轻松解决此问题。

还有人认为进行通道缓存不好,因为所有通信都将通过单个通道。请参阅以下文章。

http://social.msdn.microsoft.com/Forums/是/wcf/thread/9cbdf92a-a749-40ce-9ebe-3f2622cd78ee

是这一定是一件坏事吗?不能跨线程共享通道吗?由于对这个单一通道的多个方法调用将被串行处理,性能是否会受到影响?

我还没有发现共享频道会降低性能的证据。我发现使用缓存通道比使用非缓存通道快大约 5 倍,即使这意味着必须使用反射在缓存通道上进行方法调用。

另一个优点是,当您完成操作时,不必用 try/catch/finally 语句包围所有 WCF 调用来调用通道上的 Close()、Abort() 或 Dispose()。在我看来,WCF 强制开发人员必须管理 WCF 通道资源,这似乎朝着错误的方向迈出了一步。在 .NET Remoting 中,您使用 Activator 类创建了代理,并且无需对其执行任何操作来清理它。 .NET Framework 为您处理了所有这些事情。

I've been reading a lot of WCF articles online and it seems like most people cache the ChannelFactory objects but not the channels itself. It appears that most people are afraid to use channel caching because they don't want to handle the network faults that could render the cached channel unusable. But that could be easily fixed by catching the CommunicationException on the method, recreate the channel, and replay the method using Reflection.

Then there are people who think it's bad to do channel caching because all communication will go through a single channel. See following article.

http://social.msdn.microsoft.com/Forums/is/wcf/thread/9cbdf92a-a749-40ce-9ebe-3f2622cd78ee

Is this necessarily a bad thing? Can you not share channels across threads? Will performance suffer because multiple method calls made to this single channel will get processed serially?

I haven't found evidence that sharing channels will degrade performance. What I did find is that using a cached channel is about 5 times faster than using a non-cached channel, even if it means having to use Reflection to make the methods calls on the cached channels.

The other advantage is not having to surround all your WCF calls with try/catch/finally statements to call Close(), Abort(), or Dispose() on the channel when you are done with it. To me it seems like WCF took a step in the wrong direction by forcing developers to have to manage WCF channel resources. In .NET Remoting, you created the proxy using the Activator class and you didn't have to do anything to it to clean it up. The .NET Framework handled all of that for you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子的影子 2024-11-17 11:08:30

2 个主要原因:

  1. ChannelFactory 的创建成本很高,而且它是线程安全的 =>缓存的完美候选者。
  2. 由通道工厂生成的通道创建起来并不昂贵,但它不是线程安全的(实际上它是线程安全的,但并发调用将被阻塞并按顺序执行)=>不要在多线程环境中缓存它。

这是好文章 详细介绍了更多细节。

2 main reasons:

  1. A ChannelFactory is expensive to create and it is thread safe => perfect candidate for caching.
  2. A Channel generated by a channel factory is not expensive to create but it is not thread safe (well in reality it is thread safe but concurrent calls will be blocked and executed sequentially) => don't cache it in a multithreaded environment.

Here's a nice article which goes into further details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文