Saturday, April 1, 2017

Advanced concepts WCF listener for Service Bus

For basics of creating a WCF listener to a ServiceBus queue/topic, Pl refer -
WCF-ServiceBus Listener

1)      The default protocol used is NetMessaging (not AMQP). NetMessaging is a native protocol available in .NET only. AMQP is an OASIS standard and interoperable, meaning message sent by .NET publisher could by consumed by PHP/Python client.
2)      Unlike Service Bus, ActiveMQ offers a lot of protocols for message interchange like –
a)      Openwire (native protocol for activemq – default)
b)      Mqtt (light weight pub-sub protocol typically used for IoT)
c)      Amqp (cross platform, efficient)
3)      With the above approach, a new instance of WCF service is created each time, a message arrives the topic/queue. This was confirmed with the following code –
public interface IService
        [OperationContract(IsOneWay = true, Action = "*"), ReceiveContextEnabled(ManualControl = true)]
        void MessageReader(Message message);
public class MyService: IService
        private string instanceId = "";
        public MyService()
            instanceId = Guid.NewGuid().ToString();           
        public void MessageReader(Message message)
            var logEntry = new LogEntry { Message = string.Format("Service instance id is {0}",instanceId) };

4)      Once message arrives a topic/queue, a lock is applied. All topics/queues have a message lock timeout property, whose default value is 1 min and can be set to 5 mins as maximum value. The processing of the message has to be committed (or rolled back)  within this time or else the lock will be automatically released and the message will go back to the topic/queue, thus increasing the delivery count. Pl note that this redelivery will trigger the wcf processing again .The max delivery count can also be set. Once the message has been redelivered more than the “max delivery count”, it goes to “deadletter queue”.

  5)   With reference to the point above, the following situation can occur and should be handled carefully. Suppose a message arrives a topic, a wcf instance is created and it applies the lock on the message. The processing is message takes more than 1 minute (default lock timeout) and get redelivered to the topic/queue 10 times (default max delivery count). Ultimately all the 10 times, the message gets processed, thus causing inconsistent results. So make sure the message processing timeout (say database execution timeout or service call) is always less than message lock timeout. Also reduce the max delivery count from 10 (default) to a suitable value.