我尝试了解 WCF 如何处理实例、并发和线程。
例如,如果我将实例上下文模式配置为 PerCall,将并发模式配置为 single,则每个请求都会创建一个新的 WCF 服务实例。这与服务器端创建的线程相关意味着什么?
我猜想实例化和并发性与线程的相关性并不强。对于给定的示例,每个请求都由“主”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?
如果您使用“每次调用”实例,则服务实例都是单线程的,因为每个传入请求都由该服务的新实例提供服务。因此,在这种情况下指定并发性并不重要。
I guess that instancing and concurrency is not strong related to threading
我不确定你是如何得出这个结论的。
解决此帖子下面的评论:
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 并发模式不会影响调用处理代码的执行。
对该服务的其他调用将导致创建该服务的新实例。因此,虽然您当然可以将并发模式设置为多个,但您永远不会获得多个线程来处理对服务实例的请求。
因此,在每次调用实例中将并发模式设置为多个不会对服务的行为产生任何实际差异。
关于c# - WCF 如何处理实例化和并发性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29563276/