Creating a WCF Restful service, concurrency issues
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 U开发者_运维技巧serID)
{
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);
The best approach would be to use:
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).
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).
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>
Those are the defaults for WCF 3.5 - the maxConcurrentCalls
settings defines how many callers can be handled simultaneously.
Check out the MSDN docs on Service throttling for more details.
精彩评论