简体   繁体   English

我可以在同一个服务中使用 PerCall 实例化和可重入并发吗?

[英]Can I use PerCall instancing and Reentrant concurrency in the same service?

I've been studying the instancing and concurrency modes in WCF and I'm trying to understand if there is any case where it makes sense to use PerCall instancing and Reentrant concurrency.我一直在研究 WCF 中的实例化和并发模式,我试图了解是否有任何情况下使用PerCall实例化和可重入并发是有意义的。 In the following MSDN link it is stated that " in PerCall instancing, concurrency is not relevant ".在下面的MSDN 链接中指出“在 PerCall 实例中,并发是不相关的”。

The use of concurrency is related to the instancing mode.并发的使用与实例化模式有关。 In PerCall instancing, concurrency is not relevant, because each message is processed by a new InstanceContext and, therefore, never more than one thread is active in the InstanceContext.在 PerCall 实例化中,并发性无关紧要,因为每条消息都由一个新的 InstanceContext 处理,因此在 InstanceContext 中活动的线程永远不会超过一个。

However, I think there might be a case where these two modes (PerCall and Reentrant) must be used in conjunction.但是,我认为可能存在必须结合使用这两种模式(PerCall 和 Reentrant)的情况。 Please correct me and/or give me any input on this.请纠正我和/或给我任何意见。 Consider the following scenario:考虑以下场景:

Service A uses a Duplex MEP.服务 A 使用双工 MEP。 Service A exposes an operation contract which returns an object (ie, it is NOT a one way operation).服务 A 公开了一个返回 object 的操作合同(即,它不是单向操作)。 The service's callback contract also exposes an operation contract which returns an object (ie, it is NOT a one way operation).该服务的回调合约还公开了一个返回 object 的操作合约(即,它不是单向操作)。 The service endpoint uses a wsDualHttp Binding.服务端点使用 wsDualHttp 绑定。

The operation contract implementation calls the client's callback before returning.操作合约实现在返回之前调用客户端的回调。 In this scenario, if I set the ConcurrencyMode to Single, a Deadlock will occur , whether if I set the ConcurrencyMode to Reentrant everything works as expected .在这种情况下,如果我将 ConcurrencyMode 设置为 Single,将发生死锁,如果我将 ConcurrencyMode 设置为 Reentrant,一切都会按预期工作

So, why is Microsoft saying that concurrency is not relevant in a PerCall mode?那么,为什么微软说并发在 PerCall 模式中不相关呢?

Concurrency is bound to how many threads are accessing an Instance of the service (InstanceContext).并发性与访问服务实例 (InstanceContext) 的线程数有关。

In percall instancing, every call creates a new instance of the service and its related resources as in ASP.Net.在 percall 实例化中,每个调用都会创建一个新的服务实例及其相关资源,就像在 ASP.Net 中一样。 When the call is over, this instance would not be valid.通话结束后,此实例将无效。 More over, this instance would not be accessible by any other thread, even from the same session.此外,任何其他线程都无法访问此实例,即使是同一个 session。

So that's the reason for the statement, "in PerCall instancing, concurrency is not relevant" .所以这就是声明的原因, “在 PerCall 实例中,并发是不相关的”

Regarding your scenario , in Duplex MEP, let's say A is the client and B is the service.关于您的方案,在 Duplex MEP 中,假设 A 是客户端,B 是服务。

When A calls B, an instance of Service is created.当 A 调用 B 时,会创建一个 Service 实例。 In duplex MEP the response has to happen through a different channel (callback channel), and the execution has to jump to a different instance (client instance which you set during the proxy creation) for execution.在双工 MEP 中,响应必须通过不同的通道(回调通道)发生,并且执行必须跳转到不同的实例(您在代理创建期间设置的客户端实例)才能执行。 After completing this, it has to jump back to the original service instance and continue from where it left.完成此操作后,它必须跳回原始服务实例并从它离开的地方继续。

So in order for a duplex MEP to be successful, it needs to keep the original service's instance alive and re-enterable, when it jumps over to the callback channel for execution.因此,为了使双工 MEP 成功,当它跳转到回调通道执行时,它需要保持原始服务的实例处于活动状态并且可重新进入。 With percall instancing, this is not be possible.使用 percall 实例化,这是不可能的。

That's the reason duplex channel is looking for Reentrant/Multiple concurrency and PerSession InstanceContextMode这就是双工通道正在寻找可重入/多并发和 PerSession InstanceContextMode 的原因

Also note that when you don't specify the instance context mode, the default it PerSession.另请注意,当您不指定实例上下文模式时,默认为 PerSession。

In my opinion, the MSDN article is simplifying things.在我看来,MSDN 文章正在简化事情。 Concurrency mode does play a part in PerCall instancing mode.并发模式确实在 PerCall 实例化模式中起作用。

Check out this blog post: http://blogs.msdn.com/b/rickrain/archive/2009/06/17/wcf-instancing-concurrency-and-throttling-part-2.aspx .查看这篇博文: http://blogs.msdn.com/b/rickrain/archive/2009/06/17/wcf-instancing-concurrency-and-throttling-part-2.aspx

In the blog, the author had to use ConcurrencyMode.Multiple with PerCall, because using the default 'Single' wouldn't work well when multiple threads make calls to the service using the same proxy object.在博客中,作者不得不将 ConcurrencyMode.Multiple 与 PerCall 一起使用,因为当多个线程使用同一个代理 object 调用服务时,使用默认的“Single”将无法正常工作。

In ConcurrencyMode.Single, if the binding uses reliable session then all calls in a channel are queued even for PerCall.在 ConcurrencyMode.Single 中,如果绑定使用可靠的 session 则通道中的所有调用都将排队,即使是 PerCall。

Also, what if you have static variable in the service class?此外,如果您在服务 class 中有 static 变量怎么办? PerCall doesn't make it thread-safe, as implied by the statement 'In PerCall, concurrency is not relevant". PerCall 并没有使它成为线程安全的,正如语句“在 PerCall 中,并发不相关”所暗示的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WCF是否使用ThreadPool为PerCall服务启用新实例? - Does WCF use the ThreadPool to bring up new instances for a PerCall service? 使用设置为ConcurrencyMode.Multiple和I​​nstanceContextMode.PerCall的WCF服务行为属性时是否可能出现并发问题? - Are concurrency issues possible when using the WCF Service Behavior attribute set to ConcurrencyMode.Multiple and InstanceContextMode.PerCall? WCF:实例化和并发问题 - WCF: Instancing and Concurrency question 将ObjectCache与PerCall服务一起使用 - Using ObjectCache with PerCall service WCF:对于无状态服务,perSession是否可以优于perCall - WCF: For a stateless service, can perSession be better over perCall 使用percall实例化可能的WCF异步双工回调? - WCF async duplex callbacks possible using percall instancing? 我可以使用什么模式来避免从管道中实例化不必要的块? - What pattern can I use to avoid instancing unnecessary blocks from pipeline? 具有PerSession的WCF服务的行为类似于PerCall - WCF Service with PerSession acts like PerCall 在WCF PerCall服务上保留客户端的用户名 - Keeping the UserName of a client on WCF PerCall service Windows服务中的重入计时器 - Reentrant Timer in Windows Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM