WCF 中的并发如何工作?

发布于 2024-09-18 09:29:12 字数 155 浏览 3 评论 0原文

我是WCF和SOA的新手。我刚刚开始研究这些,我有一个理论上的疑问:

客户端 A 调用了一个服务,并且逻辑当前正在服务器上执行。当逻辑正在执行时,来自客户端 B 的另一个调用会进入同一服务。

此时客户端 A 正在执行的逻辑发生了什么?该服务如何设法满足这两个请求?

I am a novice in WCF and SOA. I am just starting out on these, I have a theoretical doubt:

Client A has called a service and the logic is currently executing on the server. While the logic is executing, another call from Client B comes in for the same service.

At this point what happens to the logic that is being executed for Client A? How does the service manage to serve both the requests?

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

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

发布评论

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

评论(5

雄赳赳气昂昂 2024-09-25 09:29:12

您的问题的答案取决于您使用的绑定。有两个设置控制此行为:InstanceContextMode 和 ConcurrencyMode。这两个设置都是在 ServiceBehaviorAttribute 中设置的。

InstanceContextMode 控制服务的实例化方式。它具有以下值:

  • PerCall - 每次调用服务时都会创建新的服务实例。这是在不使用传输会话、可靠会话或安全会话的绑定上公开的服务的默认行为=> BasicHttpBinding、WebHttpBinding。

  • PerSession - 每次从新代理实例调用服务时,都会创建新的服务实例。来自同一代理的任何后续调用都由同一服务实例(实例位于服务器上)处理。默认情况下,后续调用必须在 10 分钟内(receiveTimeout)完成,否则服务实例将被释放。这是使用传输会话、可靠会话或安全会话的绑定上公开的服务的默认行为=> WSHttpBinding(默认设置使用安全会话)、NetTcpBinding、NetNamedPipeBinding。

  • 单一 - 仅存在一个服务实例,它处理所有调用。该服务实例可以在主机启动或第一次调用服务时创建。

现在您知道如何创建实例了。第二个设置 ConcurrencyMode 控制有多少并发线程可以访问单个实例。每个请求始终在单独的线程中处理。

  • 单 - 只有一个线程可以访问服务实例。这是默认行为。

  • 可重入 - 一个线程可以访问服务,但它可以释放锁并允许其他线程使用该实例,而第一个线程将被阻塞。这用于回调场景。

  • 多 - 多个线程可以访问服务实例。

现在您知道如何并发使用实例了。让我们看一下一些组合:

  • PerCall 实例化 + 单并发 - 典型的无状态场景。允许多个并发调用。

  • PerCall 实例化 + 多个并发 - 没有意义。它的行为仍然类似于单一并发。

  • PerSession 实例化 + 单并发 - 允许多个并发调用,但只能同时处理来自每个代理的单个调用。其他调用排队。

  • PerSession 实例化 + 多个并发 - 允许多个并发调用。来自每个代理的多个调用可以同时访问同一个实例。您必须手动同步对服务实例中共享字段的访问。

  • 单实例+单并发——一次只能处理单个请求。其他请求排队(默认超时 30 秒)。

  • 单实例+多并发-允许多个并发调用。所有调用同时访问同一实例。您必须手动同步对服务实例中共享字段的访问。

Answer to your question depends on binding you are using. There are two settings controlling this behavior: InstanceContextMode and ConcurrencyMode. Both these settings are set in ServiceBehaviorAttribute.

InstanceContextMode controls how the service is instantiated. It has following values:

  • PerCall - each time you call the service new service instance is created. This is default behavior for services exposed on bindings which don't use transport session, reliable session or security session => BasicHttpBinding, WebHttpBinding.

  • PerSession - each time you call the service from new proxy instance new service instance is created. Any subsequent call from the same proxy is handled by the same service instance (instance lives on the server). By default subsequent call has to be done within 10 minutes (receiveTimeout) or service instance is released. This is default default behavior for services exposed on binding which use transport session, reliable sesion or security session => WSHttpBinding (default setting uses security session), NetTcpBinding, NetNamedPipeBinding.

  • Single - only one instance of the service exists and it handles all calls. This service instance can be created when host starts or when service is called first time.

Now you know how instances are created. The second setting ConcurrencyMode controls how many concurrent threads can access single instance. Each request is always handled in separate thread.

  • Single - only one thread can access service instance. This is default behavior.

  • Reentrant - one thread can access service but but it can release the lock and allow other thread to use the instance while firts thread will be blocked. This is used in callback scenario.

  • Multiple - multiple threads can access service instance.

Now you know how instance can be concurrently used. Lets have a look on some combinations:

  • PerCall instancing + Single concurrency - typical stateless scenario. Multiple concurrent calls are allowed.

  • PerCall instancing + Multiple concurrency - doesn't make sense. It still behaves like Single concurrency.

  • PerSession instancing + Single concurrency - multiple concurrent calls are allowed but only single call from each proxy can be processed at the same time. Other calls are queued.

  • PerSession instancing + Multiple concurrency - multiple concurrent calls are allowed. Multiple calls from each proxy can access same instance at the same time. You have to do manual synchronization of access to shared fields in the service instance.

  • Single instancing + Single concurrency - only single request can be processed at time. Other requests are queued (default timeout 30s).

  • Single instancing + Multiple concurrency - multiple concurrent calls are allowed. All calls access same instance at the same time. You have to do manual synchronization of access to shared fields in the service instance.

番薯 2024-09-25 09:29:12

这取决于应用于服务实现的 ServiceBehavior 属性的 ConcurrencyMode 属性的值。如果ConcurrencyMode为Single,则客户端B的调用将等待客户端A的调用完成;如果 ConcurrencyMode 为 Multiple,则两者将同时执行,但在不同的线程上执行。

如果未设置,ConcurrencyMode 默认为 Single:http: //msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.concurrencymode.aspx

您可能还会发现 InstanceContextMode 属性对于理解和控制来自多个客户端的多个请求的处理方式很有用:http://msdn.microsoft.com/en-us/库/system.servicemodel.servicebehaviorattribute.instancecontextmode.aspx

That depends on the value of the ConcurrencyMode property of the ServiceBehavior attribute that is applied to the service implementation. If the ConcurrencyMode is Single, the call from Client B will wait for the call from Client A to complete; if the ConcurrencyMode is Multiple, both will be executed at the same time but on separate threads.

If not set, the ConcurrencyMode defaults to Single: http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.concurrencymode.aspx

You may also find the InstanceContextMode property useful for understanding and controlling how multiple requests from multiple clients are handled: http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.instancecontextmode.aspx

眉黛浅 2024-09-25 09:29:12

实例化和并发性都有设置,请参阅 http://msdn.microsoft。 com/en-us/library/ms731193.aspx 了解详细信息。

在 IIS 中运行 WCF 服务可以解决一些并发问题。

There are settings for both instancing and concurrency, see http://msdn.microsoft.com/en-us/library/ms731193.aspx for details.

Running your WCF service in IIS takes care of some of the concurrency issues.

明明#如月 2024-09-25 09:29:12

您可以使用 ConcurrencyMode 控制并发,并通过 InstanceContextMode 控制如何处理新连接 - 这是 微软文档

并发连接数也可以被限制 - 请查看 serviceThrotdling 元素。

You can control the concurrency using the ConcurrencyMode and control how new connections are handled via InstanceContextMode - this is the Microsoft documentation.

The number of concurrent connections can also be throttled - have a look at the serviceThrottling element in the your WCF Config.

深海里的那抹蓝 2024-09-25 09:29:12

那么服务正在单独的线程中执行请求。

所以他们可能会同时被处决。

Well the service is executing the requests in seperate Threads.

So they might get executed at the same time.

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