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 …

Entity Framework (EF) TransactionScope vs Database.BeginTransaction

In today blog post we will talk a little about a new feature that is available on EF6+ related to Transactions.
Until now, when we had to use transaction we used ‘TransactionScope’. It works great and I would say that is something that is now in our blood.
using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (SqlConnection conn = new SqlConnection("...")) { conn.Open(); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = conn; sqlCommand.CommandText = ... sqlCommand.ExecuteNonQuery(); ... } scope.Complete(); } Starting with EF6.0 we have a new way to work with transactions. The new approach is based on Database.BeginTransaction(), Database.Rollback(), Database.Commit(). Yes, no more TransactionScope.
In the followi…

GET call of REST API that contains '/'-slash character in the value of a parameter

Let’s assume that we have the following scenario: I have a public HTTP endpoint and I need to post some content using GET command. One of the parameters contains special characters like “\” and “/”. If the endpoint is an ApiController than you may have problems if you encode the parameter using the http encoder.
using (var httpClient = new HttpClient()) { httpClient.BaseAddress = baseUrl; Task<HttpResponseMessage> response = httpClient.GetAsync(string.Format("api/foo/{0}", "qwert/qwerqwer"))); response.Wait(); response.Result.EnsureSuccessStatusCode(); } One possible solution would be to encode the query parameter using UrlTokenEncode method of HttpServerUtility class and GetBytes method ofUTF8. In this way you would get the array of bytes of the parameter and encode them as a url token.
The following code show to you how you could write the encode and decode methods.