创建WCF Restful服务,并发问题

时间:2011-01-15 09:39:24

标签: wcf web-services rest

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

以下是我目前的样本;

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

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

    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);

1 个答案:

答案 0 :(得分:7)

最好的方法是使用:

  • InstanceContextMode.PerCall
  • ConcurrencyMode.Single

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

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

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

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

这些是WCF 3.5的默认设置 - maxConcurrentCalls设置定义了可以同时处理多少呼叫者。

查看MSDN docs on Service throttling了解详情。

相关问题