locked
WCF PerCall instance with multiple threads? RRS feed

  • Question

  • User1224379429 posted

    Hi

    I have a requirement to develop service that handles multiple requests. Client wants to send more that 100 requests at a time. 

    Can I use the below attributes to handle multiple requests with multiple threads? Are there any drawbacks? Please share your experience. Thanks

    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall, 
                     ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class HelloWorldService : IHelloWorldService
    {
    }
    Tuesday, February 21, 2017 9:07 AM

All replies

  • User475983607 posted

    IIS and WCF handles multiple concurrent requests by default.  There is really no way to know if the attributes you selected are correct because we don't know the specification of your service.  However, ConcurrencyMode = ConcurrencyMode.Multiple is probably not what you want.

    https://msdn.microsoft.com/en-us/library/ms731193(v=vs.110).aspx

    What problem are you trying to solve and are you receiving errors or unexpected results.  If so, what are the errors and unexpected results.

    Are you receiving errors while stress testing the service?  If so, is the client explicitly closing connections?  This is by far the biggest issue I see with WCF when under a load is the client not closing connections and handling faults.   What happens is the connections queue and the application slows down while it waits of connections to open.

    Tuesday, February 21, 2017 12:48 PM
  • User-2057865890 posted

    Hi Athar,

    When the service is set to Multiple Concurrency Mode, the service allows multiple accesses at the same time. Each service has multiple threads processing messages concurrently. The service implementation must be thread-safe to use this concurrency mode.

    reference: 

    #WCF Concurrency (Single, Multiple, and Reentrant) and Throttling

    https://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and

    Best Regards,

    Chris

    Wednesday, February 22, 2017 2:43 AM
  • User1224379429 posted

    HI

    Currently what's happening is Web Service gives different responses to the same request. I have a requirement to create a web service to return Exchange rates from Database against the FromCurreny and ToCurrency. User pass two input parameters, web service fetch the Exchange Rate and reply as a Class object. To avoid different response issue "Lock" has been implemented on every request and it makes other requests wait. 

    I want to enhance and convert the same to WCF service to handle multiple client requests and make sure request gets the relevant response. 

    Thanks

    Wednesday, February 22, 2017 7:06 AM
  • User475983607 posted

    Of course you must use a lock because you configured WCF to use one instance for multiple requests.  You need the lock because local member variable are shared across every request.  Why would purposely configure WCF this way given your requirement?

    Simply use the default configuration (remove the attributes) and WCF/IIS should work fine because each request will get a new instance of the WCF class. I generally throttle the service to get the most performance while protecting the service.

    https://msdn.microsoft.com/en-us/library/ms735114(v=vs.100).aspx

    Wednesday, February 22, 2017 11:27 AM