Home > Azure > Azure Service Bus

Azure Service Bus

In this post I am going to walk you through the Azure Service Bus, as I am very passionate about Service Bus Architecture and did some posting on it NService Bus in the past. If you are new to Service Bus and want to understand integration using Service Bus architecture or SOA then I suggest read my previous following post.

  • So first thing you need is to browse the Azure management portal at https://manage.windowsazure.com and sign up for the windows azure services.Once you have signed in the management portal you are all set to configure the service bus as shown below.

    Create namespace for service bus.

    Lets create a namespace and I am going to create namespace prashantbrall.servicebus.windows.net and choose “East Asia” as my data center as this is the closest data center to Australia. Currently there is no Azure data center in Australia but what I have heard is that Microsoft is building a new one and will be operational in 6 months 🙂

    Add namespace

    Now the namespace is created so lets click on the connection information to get the connection string for the endpoint of this service bus, as this connection string will be required in couple of places.

    Service Bus Endpoint

    Also before starting any serious service bus development I highly recommend downloading the service bus explorer from Pablo Salvatori’s sky drive, it’s a great tool to view queues, messages and resend messages.

    Lets start with File>New Console application.

    File New Console Application

    Now we will add the Azure service bus reference using Nuget Package Manager.

    Add Windows Azure Service Bus using Nuget

    And just to see what packages Nuget has added to solution lets expand the “reference” tree in the Solution Explorer and as you can see it has added the two assemblies Microsoft.ServiceBus and Microsoft.WindowsAzure.Configuration

    Windows Azure Service Bus references.

    Remember a couple of steps above I told you save the connection string to the Service Endpoint and now is the time to add this connection string in our app.config and this is how the connection strings looks like.

    Adding service bus enpoint to the app.config file

    Lets configure our Service Bus Explorer to view our queues by adding the same connection string to the Service Bus Explorer.

    Connection string in Service Bus Explorer.

    Lot of configuration and basic Azure Service Bus stuff lets write some code and start sending some messages to the queue. So first thing I am going to do is to declare a simple POCO class OrderMessage which we will send to the OrderQueue.


    public class OrderMessage 
        public int OrderId { get; set; }
        public int Qty { get; set; }

    Now we will wire up some simple code to check whether the Queue exists or not and if not then create the queue and this is all done using the NamespaceManager class provided by the Azure Service Bus. Once we have created the queue then we will use the QueueClient to create a client and send the message to the OrderQueue.And here is the code


    class Program
        const string QueueName = "OrderQueue";
        static void Main(string[] args)
            var namespaceManager = NamespaceManager.Create();
            Console.WriteLine("\nCreating Queue '{0}'...", QueueName);
            if (!namespaceManager.QueueExists(QueueName))
            var queueClient = QueueClient.Create(QueueName);
            var newOrder = new OrderMessage { OrderId = 123, Qty = 20 };
            queueClient.Send(new BrokeredMessage(newOrder));

    And now you can see our “OrderQueue” in the service bus explorer as shown below.

    View Order Queue in Service Bus Explorer

    Lets see the if the message is there on the queue by peeking this message as we do not want this message to be processed and removed from the queue.Peeking is very good practice as you do not want to accidentally process a message in your production system.

    Peek message from Order Queue

    As you can see this message has a MessageId which is an internal Guid and used by Azure Service Bus for tracking messages and some addition properties like when this message was created etc. However, the content of the message which looks a bit odd as the value of these field contains gibberish. This has happen as a result of using the binary formatter when the message was serialized and send to the queue. It is easy to change the formatter of your choice and I personally like JSON formatter.

    View Message in Service Bus Explorer

    The queue can also be viewed using the Azure Management Portal, however the only thing you can see is a list of queues and the current size of the messages in bytes. This is the main reason why we used the Service Bus Explorer as it gives more information about the message.

    View of Queues through Management Portal

    So far so good and lets receive the message using the QueueClient.

    Program.cs (contd…)

    var queueClient = QueueClient.Create(QueueName);
    BrokeredMessage message = null;
        message = queueClient.Receive();
        if (message != null)
            var orderMessage = message.GetBody<OrderMessage>();
            Console.WriteLine("Order with Id = {0} and Qty = {1} has been received..", 
                    orderMessage.OrderId, orderMessage.Qty);

    and here is the output of the message on console window.

    Order Message received output on console window

    You can also configure some important attributes of the queue using the management portal as show below. And depending on your requirements you may want to configure some important things such as number of retry, duplicated detection of messages or enable Dead Letter Sub Queue for failed and poisoned messages.

    Order Queue Configuration through management portal

    That’s it for today and happy Azuring !!!


    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )


    Connecting to %s

    %d bloggers like this: