只允许一个客户端并拒绝其他客户端的服务

只允许一个客户端并拒绝其他客户端的服务

本文介绍了WCF:只允许一个客户端并拒绝其他客户端的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个一次只允许一个客户端的 WCF 服务.所有其他请求都应该被拒绝,客户端必须稍后重试.该服务大约需要一分钟才能完成请求.

I need to create a WCF service that only allows a single client at a time. All other requests should be rejected, and the client must retry again later. The service will take around a minute to complete the request.

我试过了:

 [ServiceBehavior(IncludeExceptionDetailInFaults=true,
                  InstanceContextMode=InstanceContextMode.Single,
                  ConcurrencyMode=ConcurrencyMode.Single)]

但是如果我在客户端多次请求服务(使用多个线程),我会在第 3 次请求时收到超时异常,但该服务确实执行了第 3 次请求处理!

but if I request the service multiple times (using multiple threads) in a client, I get a timeout exception on the 3rd request, but the service does actually perform the 3rd requests processing!

推荐答案

您还可以查看ServiceThrottling"行为,它允许您在服务器端指定您想要多少并发实例、并发调用和并发会话支持 - 将它们全部设置为 1,您应该拥有自己的行为.

You could also check out the "ServiceThrottling" behavior, which allows you to specify on the server side how many concurrent instances, concurrent calls and concurrent sessions you want to support - set them all to 1, and you should have your behavior.

<behaviors>
  <serviceBehaviors>
     <behavior name="Throttled">
         <serviceThrottling maxConcurrentCalls="1"
            maxConcurrentSessions="1"
            maxConcurrentInstances="1" />
     </behavior>
  </serviceBehaviors>
</behaviors>

马克

这篇关于WCF:只允许一个客户端并拒绝其他客户端的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:55