创建WCF Restful服务,并发问题

pmi*_*lio 5 rest wcf web-services

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

这是我到目前为止的样本;

[ServiceBehavior(IncludeExceptionDetailInFaults = true, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)] 
Run Code Online (Sandbox Code Playgroud)

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

    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);
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 7

最好的方法是使用:

  • InstanceContextMode.PerCall
  • ConcurrencyMode.Single

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

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

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

<serviceBehaviors>
  <behavior name="Throttling">
     <serviceThrottling 
             maxConcurrentCalls="16"
             maxConcurrentInstances="16"
             maxConcurrentSessions="10" />
  </behavior>
</serviceBehaviors>
Run Code Online (Sandbox Code Playgroud)

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

有关更多详细信息,请查看有关服务限制MSDN文档.