Skip to main content

Service Bus Queues from Windows Azure - How to use it

This is the second post about Service Bus Queues from Windows Azure. In the first one I made a small introduction to Service Bus Queues. Today we will see how we can create a new queue in Service Bus namespace and how we can use it.
So, first of all you need a Windows Azure account. After you enter Windows Azure portal we need to go to Services tab and select Service Bus. On this page we will need to create a new namespace for our service. The namespace is a unique name that is used to identify a unique service (in our case the Service Bus). After we create this namespace, we already have the Service Bus Queue created automatically. In this moment we don’t need to pay for anything because we didn’t create a queue.
To be able to access the queue we need the “Default Issuer” and “Default Key”. These two strings represent our security credentials that can be used to access the queue from our application. These values can be found if we select the Service Bus namespace that we created already and click on the “View” button from the “Default Key” panel (it is in the right of the page).
Now, that we have this credentials we need to add them to our applications. To be able to access the Service Bus Queue we will security information plus the endpoint (the namespace to our Service Bus).
There are a lot of places where we can store these values. The simplest one is to hardcode this value in our application, but is not the best one. If we create a website we can use configuration file of our web application to store this information.
value="Endpoint=sb://;SharedSecretIssuer=myFooIssuerName;SharedSecretValue=myFooDefaultKey" />
In the case you are working with a web-role or with a worker role, a good idea is to save this information in the service definition file (*.csdef and *.cscfg).
<WebRole name="FooWebRole" vmsize="Medium">
<Setting name="ServiceBusConnectionString" />

<Role name="FooWebRole"
<Setting name="ServiceBusConnectionString"
value=="Endpoint=sb://;SharedSecretIssuer=myFooIssuerName;SharedSecretValue=myFooDefaultKey" />
Another possible solution is to store this information in a separately file, that can be consume by our application. In this case when we will create the instance of Service Bus Queue we will need to be able to specify this data.
So now, we can look in the code to see what we need to do (write). After we add the namespaces that are required (Microsoft.ServiceBus and Microsoft.ServiceBus.Messaging) we can create a new instance to our namespace manager. Using this manager we will be able to create new Service Bus Queues and access them.
NamespaceManager nm = NamespaceManager.CreateFromConnectionString(
You will see that we have a lot of ways to create a queue. It is important at this step to know how we can specify custom properties to a queue. Before creating the queue we can have a QueueDescription object where we can define the name of the queue, the maximum size, the default time to live and so on. Using this object we can create a queue with custom setup. If we want the default queue we can specify only the name of the queue.
QueueDescription qd = new QueueDescription("FooQueue");
qd.MaxSizeInMegabytes = 5120;
qd.DefaultMessageTimeToLive = new TimeSpan(0, 10, 30);
if (!namespaceManager.QueueExists("FooQueue"))
// or namespaceManager.CreateQueue(“FooQueue”);

Don’t forget to check if the queue exists before creating the queue. After we create a Service Bus Queue we don’t need any more the NamespaceManager instance. Using the connection string and the name of the queue we will be able to create a new instance of the QueueClient object that is able to communicate with our queue.
QueueClient qc =
myFooConnectionString, "FooQueue");
BrokeredMessage message = qc.Receive();
You will ask me what is the BrokeredMessage? This is the default message that is send and returned by a queue. This kind of message contains two parts:
  • Heather – where all the configuration properties are stored (like label and time-to-live) + custom properties that we want to set. The maxim size of the heather is 64kb.
  • Body – the message body that can be any type of item that is serialized using DataContractSerializer or an IO stream.
Don’t forget that we have a current limit of 256kb per message (in the near future this limit will increase to 1MB maybe ).
When we try to consume a message from the queue we have to options. We can get a message to the queue in a transactional manner. Basically we get the message from the queue and after that we need to confirm that we processed the message (the default timeout is 60 seconds). In this period of time the message will be locked in the queue and no other receiver will be able to process it. Note: the message is not removed from the queue, is only locked. If the consumers don’t notify the queue that he processed the message, it will be unlocked and other consumers will be able to consume the message.
BrokeredMessage message = qc.Reiceve();
// Do some stuff with the message.
message.Complete(); // Notify the queue that the message was processed.
The default period of time that we have to notify the queue that we processed the message is 60 seconds. If we want to notify the queue that we abandoned the processing of that message we can call message.Abadon(). This kind of messaging processing is named PeerLock mode and it requests two calls from the consumer to the queue. Because of this we can increase a little the numbers of transactions.
Another approach that can be used is to extract the message from the queue and automatically delete the message from the queue. In this way any message that is extracted from the queue is deleted automatically.
var message = qc.ReceiveAndDelete();
Be aware that when you call ReceiveAndDelete or Receive method, the code will freeze at that line of code until a message is available. We can specify a timeout if we want.
In this blog we saw how we can use Service Bus Queues in our applications. In the next step I will describe a mechanism on how we can iterate through without consuming the messages.


Popular posts from this blog

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

How to check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

Fundamental Books of a Software Engineer (version 2018)

More then six years ago I wrote a blog post about fundamental books that any software engineer (developer) should read. Now it is an excellent time to update this list with new entries.

There are 5 different categories of books, that represent the recommended path. For example, you start with Coding books, after that, you read books about Programming, Design and so on.
There are some books about C++ that I recommend not because you shall know C++, only because the concepts that you can learn from it.


Writing solid codeCode completeProgramming Pearls, more programming pearls(recommended)[NEW] Introduction to Algorithms


Refactoring (M. Fowler)Pragmatic ProgrammerClean code[NEW] Software Engineering: A Practitioner's Approach[NEW] The Mythical Man-Month[NEW] The Art of Computer Programming


Applying UML and Patterns (GRASP patterns)C++ coding standards (Sutter, Alexandrescu)The C++ programming language (Stroustrup, Part IV)Object-oriented programming (Peter Coad)P…