Processing FIFO MSMQ Messages using WCF

 

I have been having fun working on the middle tier of an application which is using BizTalk, Windows Workflow, MSMQ and the Windows Communication Framework.

 

One of the requirements is to processes messages we receive from a legacy system through MSMQ in FIFO order. 

 

We used WCF to communicate with MSMQ and used the msmqIntegrationBinding binding since the legacy application was placing the messages on the queue.

 

The FIFO processing seemed to be little more difficult to iron out.  It turns out that by default a service's InstanceContextMode is PerSession.  If the channel is a datagram then the IntanceContextMode degrades to PerCall.  So what happens is that the WCF runtime will create a new service instance for each available request/message up to the MaxConcurrentCalls/MaxConcurrentInstance setting utilizing multiple threads.  This was not going to provide my FIFO processing.

 

So, to set WCF to only pull and process one message at a time you need to set the service's InstanceContextMode to Single along with setting the ConcurrencyMode to single.  The following code shows these settings:

 

        [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,

        ConcurrencyMode = ConcurrencyMode.Single,

         )]

    public class MSMQEventService : IMSMQEventInterface, IDisposable

    {

 

Once these settings are set you will achieve FIFO processing and WCF will only utilize a single thread and will process one message at a time from the single queue.

 

My next entry will cover processing multiple queues and processing each queue in FIFO order.