本文介绍了WCF ClientBase线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了ClientBase以使用WCF连接到服务.然后,我在通道上调用一种方法来与服务进行通信.

I have implemented ClientBase to use WCF to connect to a service. I'm then calling a method on the channel to communicate with the service.

base.Channel.CalculateSomething();

此调用线程安全吗?或者在运行多个线程时应该锁定它吗?

Is this call thread safe or should I lock around it when running multiple threads?

谢谢

推荐答案

关于此处答案的后续评论也使我不确定,因此我做了一些更深入的研究.这是一些可靠的证据,表明ClientBase<T>是线程安全的-讨论了如何在单个客户端代理同时被多个线程使用的情况下使WCF服务正确运行(粗体强调是原始):

The follow-up comments on the answers here had me uncertain as well, so I did some more digging. Here is some solid evidence that ClientBase<T> is thread-safe - this blog post discusses how to make a WCF service perform properly in the presence of a single client proxy being used by multiple threads simultaneously (the bold emphasis is in the original):

  1. 客户端是多线程的,并且正在使用同一代理从多个线程对您的服务进行调用.

  1. The client is multi-threaded and is making calls to your service from multiple threads using the same proxy.

客户端和服务之间的绑定是具有会话的绑定(例如,netTcpBinding,带有可靠会话的wsHttpBinding,netNamedPipeBinding等).

The binding between the client and the service is a binding that has session (for example, netTcpBinding, wsHttpBinding w/Reliable Session, netNamedPipeBinding, etc.).

此外,这篇文章中的证据似乎与Brian所说的WCF将所有多线程请求 serialize 序列化的说法相矛盾.该帖子显示了同时运行 -如果使用 ConcurrencyMode.MultipleInstanceContextMode.PerCall的单个客户端的多个请求.

Also, the evidence in this post seems to contradict Brian's additional remark that WCF serializes any multi-threaded requests. The post shows multiple requests from a single client running simultaneously - if ConcurrencyMode.Multiple and InstanceContextMode.PerCall are used.

还有一些其他讨论有关此方法的性能含义以及一些替代方法.

There is some additional discussion here regarding the performance implications of this approach as well as some alternatives.

这篇关于WCF ClientBase线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:24
查看更多