WCF 如何处理实例化和并发性?

How does WCF handle instancing and concurrency?

我试图了解 WCF 如何处理实例化、并发和线程。

例如,如果我将实例上下文模式配置为 PerCall 并将并发模式配置为 singe,则每个请求都会创建一个新的 WCF 服务实例。这与创建服务器端的线程相关是什么意思?

我猜实例化和并发与线程关系不大。对于给定的示例,每个请求都由 "main" I/O 线程池排队等待工作线程执行,这对吗?还是每个请求都创建一个新的实例和线程来执行这个请求?

if I configure the instance context mode to PerCall and concurrency mode to singe, every request creates a new instance of the WCF service. What does that mean related to the threads, which a created server side?

如果您使用 Per-Call 实例化,那么服务实例都是单线程的,因为每个传入请求都由服务的新实例提供服务。因此在这种情况下指定并发性不相关。

I guess that instancing and concurrency is not strong related to threading

我不确定你是怎么得出这个结论的。


解决下面的评论post:

In a Per-Call mode, for each call we create an instance of the service. If we are in a single thread mode, there is one thread handling these instances so we serve requests in a sequential way

这两种说法是矛盾的。如果我们为每个请求创建一个新实例,那么该服务不会按顺序处理多个请求。它将根据请求创建一个新实例。

(In PerCall mode) if the concurrency mode is multiple, there are many threads executing the instances methods so we serve request in parallel way

这是不正确的。多个请求将分别由各自的服务实例处理。

处理服务实例中的调用需要一个线程。调用处理代码很可能是多线程的,但这是服务内部的,WCF 并发模式不会影响调用处理代码的执行。

对该服务的其他调用将导致创建该服务的新实例。因此,虽然您当然 可以 将并发模式设置为多个,但您永远不会得到一个以上的线程来处理对服务实例的请求。

因此,在每个调用实例中将并发模式设置为多个不会对服务的行为产生任何实际影响。