multithreadingwcfservicesingletonservicebehavior

Multithreaded Singleton in a WCF - What is the appropriate Service Behavior?


I have a class (ZogCheckPublisher) that implements the multithreaded singleton pattern. This class is used within the exposed method (PrintZogChecks) of a WCF service that is hosted by a Windows Service.

public class ProcessKicker : IProcessKicker
{
   public void PrintZogChecks(ZogCheckType checkType)
   {    
       ZogCheckPublisher.Instance.ProcessCheckOrCoupon(checkType);
   }
}

ZogCheckPublisher keeps track of which 'checkType' is currently in the process of being printed, and rejects requests that duplicate a currently active print request. I am trying to understand ServiceBehaviors and the appropriate behavior to use. I think that this is appropriate:

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

One instance of the service that is multithreaded. If I am understanding things rightly?


Solution

  • Your understanding is correct.

    The service behavior will implement a single multithreaded instance of the service.

    [ServiceBehaviorAttribute(Name = "Test", InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple]
    

    In a singleton service the configured concurrency mode alone governs the concurrent execution of pending calls. Therefore, if the service instance is configured with ConcurrencyMode.Multiple, concurrent processing of calls from the same client is allowed. Calls will be executed by the service instance as fast as they come off the channel (up to the throttle limit). Of course, as is always the case with a stateful unsynchronized service instance, you must synchronize access to the service instance or risk state corruption.

    The following links provide additional Concurrency Management guidance:
    Multithreaded Singleton WCF Service
    http://msdn.microsoft.com/en-us/library/orm-9780596521301-02-08.aspx

    Regards,