How to receive message from Azure Service Bus Queue without polling a queue


  • Hi,
    Looking for samples on how to subscribe to a service bus queue so that whenever a new message is added to the queue it calls a callback method and doesn't need to keep polling the queue in infinite loop.

    -Sachin Sancheti

    Saturday, February 25, 2012 11:24 PM


  • We only charge for payload messages, Sachin. The WCF programming model does exactly the same thing; it maintains a loop.

    Hope that helps

    Sunday, February 26, 2012 2:49 AM

All replies

  • The Service Bus .NET API is full duplex. It's "pull", but not "poll".

    We register your wish to receive a message on the broker and service that request once we have a message. If that happens 30 minutes after you called BeginReceive, that message comes down the socket 30 minutes later. In the meantime we maintain that socket with periodic pings that keep it from timing out given that the Windows Azure load balancer (and other infrastructures) impose a 60s idle timeout. So we're not polling with the binary protocol used by the .NET client.

    If you call Receive/BeginReceive() with a timeout > 60s we're doing all that for you under the hood.

    What you really may be asking for is a push programming model. You can get that out-of-the-box with WCF and the NetMessagingBinding. There you just write a service and have messages be dispatched on that service and WCF does the receiving for you.

    That said, receiving messages explicitly in a loop has a significant number of advantages including load leveling and load balancing.

    I would strongly recommend that you take an hour to watch this talk:

    Best regards

    Sunday, February 26, 2012 12:31 AM
  • Thanks Clemens.
    What a coincidenc!! I asked that question after watching your Build videos, awesome videos.

    Right, i am looking for push programming model,since a receive against an empty queue is considered a billable message. Yes, I agree for load leveling a loop is required.

    It would be great if you could point to the samples which implements push programming model with Azure AppFabric Service Bus Queue.

    Also are the code samples which you used in videos available somewhere?

    Thanks a ton, Service Bus is Super Cool. :)

    -Sachin Sancheti

    Sunday, February 26, 2012 1:10 AM
  • We only charge for payload messages, Sachin. The WCF programming model does exactly the same thing; it maintains a loop.

    Hope that helps

    Sunday, February 26, 2012 2:49 AM
  • Hi Clemens - when you say you only charge for payload messages, are you saying that a while loop which invokes the QueueClient BegingReceive/EndReceive methods should not create a charge on the account?  I used this for the first time yesterday (3 month trial) and very quickly spent more than the free 500,000 messages.  Just leaving my test worker project running, another 1 million messages were charged for.  This suggests that a receive only ends up as a payload/chargeable message?
    Thursday, December 13, 2012 2:52 PM
  • @Sachin. I realise this is an old thread but for those who may read this.
    I think your testing may have been flawed. It reads like you were just polling the Service Bus queue & not taking advantage of its ability to deliver messages to you. Note: as of Feb 2014, the Azure Storage Queue, can only be polled whereas the Service Bus queue can support "long polling"

    You get charged for all receives. Including those that are empty messages. You do not get charged for any internal items like "keep-alive" messages to other Azure infrastructure.

    The trick to keeping your costs down is to set long-ish  time-out on the Receive/OnMessage method. The Default = 60 secs.

    If a message arrives before the timeout, you pay for it. If it doesn't you pay for the empty message.

    • Proposed as answer by Noel Abrahams Friday, May 13, 2016 6:52 AM
    Saturday, March 1, 2014 10:27 AM