创建 WCF Restful 服务,并发问题 [英] Creating a WCF Restful service, concurrency issues

查看:35
本文介绍了创建 WCF Restful 服务,并发问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用 WCF 创建一个宁静的服务,该服务在任何给定时间都可能被至少 500 人使用.我需要设置什么设置才能处理这个问题.请给我任何要点和提示,谢谢.

Hi i am in the process of creating a restful service with WCF, the service is likely to be consumed by at least 500 people at any given time. What settings would i need to set in order to deal with this. Please give me any points and tips, thanks.

这是我目前所拥有的样本;

Here is a sample of what i have so far;

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

这是一个方法被调用的例子;

And this is an example of a method being called;

    public UsersAPI getUserInfo(string UserID)
    {
        UsersAPI users = new UsersAPI(int.Parse(UserID));

        return users;
    }



    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "User/{UserID}")]
    [WebHelp(Comment = "This returns a users info.")]
    UsersAPI getUserInfo(string UserID);

推荐答案

最好的方法是使用:

  • InstanceContextMode.PerCall
  • ConcurrencyMode.Single

这将为每个调用者创建一个服务类的新实例,并使您不必担心对代码的多线程并发访问,因为每个请求都有自己的服务类实例(它本身是单-线程 - 它一次只服务一个调用者).

This will create a new instance of the service class for each caller and saves you from having to worry about multi-threaded, concurrent access to your code, since each request gets its own service class instance (which in itself is single-threaded - it serves only a single caller at a time).

此外,通过这种方法,您可以轻松地横向扩展",例如只需添加更多服务器来处理更高的负载(您所在位置的服务器,或在云中"的服务器,例如 Windows Azure 工作人员).

Also, with this approach, you can easily "scale out", e.g. just simply add more servers to handle higher load (servers at your locations, or "in the cloud", e.g. Windows Azure workers).

使用 ServiceThrottling 服务行为,您可以非常轻松地控制允许的并发调用者数量 - 这取决于您的机器的类型和大小.

Using the ServiceThrottling service behavior, you can control very easily how many concurrent callers are allowed - this depends on the type and size of your machine.

<serviceBehaviors>
  <behavior name="Throttling">
     <serviceThrottling 
             maxConcurrentCalls="16"
             maxConcurrentInstances="16"
             maxConcurrentSessions="10" />
  </behavior>
</serviceBehaviors>

这些是 WCF 3.5 的默认设置 - maxConcurrentCalls 设置定义了可以同时处理的调用者数量.

Those are the defaults for WCF 3.5 - the maxConcurrentCalls settings defines how many callers can be handled simultaneously.

查看 有关服务限制的 MSDN 文档 了解更多详情.

Check out the MSDN docs on Service throttling for more details.

这篇关于创建 WCF Restful 服务,并发问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆