WCF - 处理来自多个客户端的请求

发布于 2024-08-07 17:43:45 字数 178 浏览 6 评论 0原文

我的 WCF 服务库作为 Windows 服务托管,应该处理来自多个客户端的请求。 客户经常提出的一项请求是相当资源密集型的。

我对上述场景有两个疑问:

  1. WCF服务如何处理多个客户端的请求?
  2. 是否有任何 WCF 配置可以提高该过程的效率?

谢谢你!

My WCF service library is hosted as a Windows Service and is supposed to handle requests from multiple clients.
One request that clients are going to make frequently is pretty resource intensive.

I have two doubts pertaining to above scenerio:

  1. How a WCF service handles multiple client's request?
  2. Is there any WCF configuration to make the process efficient?

Thank you!

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

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

发布评论

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

评论(1

安静 2024-08-14 17:43:45

在默认情况下,WCF 服务主机(托管服务类的事物)将为每个传入的请求创建服务类的新实例,并让其处理该请求(“每次调用”激活)。

您可以使用服务器上的 serviceThrotdling 行为来调整并发活动服务类实例的最大数量。

<system.serviceModel>
   <behaviors>
      <serviceBehaviors>
         <behavior name="ThrottledServiceBehavior">
            <serviceThrottling 
                maxConcurrentCalls="25" 
                maxConcurrentSessions="25"
                maxConcurrentInstances="25"/>
         </behavior>
      </serviceBehaviors>
   </behaviors>

Kenny Wolf 的博客文章在这里。

此外,服务类(实现服务协定)上的 InstanceContextModeConcurrencyMode 设置对您的服务处理并发和多个请求的方式有很大影响。

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall, 
                 ConcurrencyMode=ConcurrencyMode.Single)]
class YourServiceClass : IYourService
{
  .....
}

InstanceContextMode 应为 PerCall(每个调用请求都会获得一个新的单独实例),然后 ConcurrencyMode 可以为 Single (这是最容易开发的)。

如果您需要基于会话的方法(不是很常见),InstanceContextMode 也可以是 PerSession,或者 Single(您的服务类将是单例 -强烈建议不要使用此功能,除非您绝对必须、必须了解它的所有怪癖和问题!)。

ConcurrencyMode 也可以是 Reentrant (仅与双工合约和绑定相关)或 Multiple (多线程单例服务类 - 高风险且难以开发! )。

马克

In your default scenario, the WCF service host (the thing hosting your service class) will create a new instance of your service class for each request that comes in, and lets that handle the request ("per-call" activation).

You can tweak the maximum number of those concurrently active service class instances using the serviceThrottling behavior on your server.

<system.serviceModel>
   <behaviors>
      <serviceBehaviors>
         <behavior name="ThrottledServiceBehavior">
            <serviceThrottling 
                maxConcurrentCalls="25" 
                maxConcurrentSessions="25"
                maxConcurrentInstances="25"/>
         </behavior>
      </serviceBehaviors>
   </behaviors>

There's a really good explanation of the options of the service throttling behavior and its default values in Kenny Wolf's blog post here.

Also, setting of the InstanceContextMode and ConcurrencyMode on your service class (that implements the service contract) have a strong influence on how your service will handle concurrency and multiple requests.

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall, 
                 ConcurrencyMode=ConcurrencyMode.Single)]
class YourServiceClass : IYourService
{
  .....
}

InstanceContextMode should be PerCall (each calling request gets a new, separate instance) and then ConcurrencyMode can be Single (which is the easiest to develop).

InstanceContextMode could also be PerSession if you need a session-based approach (not very common), or Single (your service class would a singleton - highly discouraged to use this, unless you absolutely, positively have to and know about all the quirks and problems with it!).

ConcurrencyMode could also be Reentrant (only relevant for duplex contracts and bindings) or Multiple (multithreaded singleton service class - highly risky and difficult to develop!).

Marc

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