Search code examples
c#wcfmessage-queuewcfserviceclientrequest-queueing

User request queue in WCF


I have a WCF service which creates a number of files at a server location doing various calculation on seed file depending upon the params given. The problem is that, the when 2 or more clients try to do calculation on same seed file, it is returning error. The cause is simply due to read/write access by multiple users at a time. So I want to create a user request queue in WCF from where server does its calculation one at a time and returns calculated response to the user. The problem is I dont know how to do it.

I have not implemented any request queue technique in WCF before. Does anyone know how to implement this in WCF Sevcices. I cannot do threading as calculation depends upon the file I/O so handling one request at a time is only one solution at this time.

Any tutorial or video tutorial will be highly appreciated.


Solution

  • Finally I did it. Here I am posting my soluton for other users who may be new to the WCF request queuing. At first, we need to implement the throttling settings in WCF host file. Throttling can be done in two ways (Either way is OK):

    1. Config file
    2. Code

    Throttling settings in config file as follows:

    [behaviors] [serviceBehaviors] [behavior name="throttlingBehavior"] [serviceThrottling maxConcurrentCalls="3" maxConcurrentInstances="3" maxConcurrentSessions="100"/] [/behavior] [/serviceBehaviors] [/behaviors]

    Or throttling settings in code

    using (ServiceHost host = new ServiceHost(typeof(SimpleService.SimpleS­ervice)))
    { 
    ServiceThrottlingBehavior throttlingBehavior = new ServiceThrottlingBehavior { MaxConcurrentCalls = 3, MaxConcurrentInstances = 3, MaxConcurrentSessions = 100 }; 
    host.Description.Behaviors.Add(throttlin­gBehavior); 
    host.Open(); 
    Console.WriteLine("Host started @ " + DateTime.Now.ToString()); 
    Console.ReadLine();
    }
    

    With the above throttling settings a maximum of 3 concurrent calls are processed. In addition to maxConcurrentCalls property, maxConcurrentInstances and maxConcurrentSessions may also impact the number of calls processed concurrently.

    Now after defining throttling behavior, we need to define concurrency mode in service contract as follows:

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,ConcurrencyMode=ConcurrencyMode.Multiple)]
    public class Service:IService
    {...
    

    With these settings in placed, we can easily get the the request queuing in WCF service.