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.
<configuration>
<appSettings>
<add
key="ServiceBusConnectionString"
value="Endpoint=sb://myFooNamespace.servicebus.windows.net/;SharedSecretIssuer=myFooIssuerName;SharedSecretValue=myFooDefaultKey" />
</appSettings>
</configuration>
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">
<ConfigurationSettings>
<Setting name="ServiceBusConnectionString" />
</ConfigurationSettings>
</WebRole>

<Role name="FooWebRole"
<ConfigurationSettings>
<Setting name="ServiceBusConnectionString"
value=="Endpoint=sb://myFooNamespace.servicebus.windows.net/;SharedSecretIssuer=myFooIssuerName;SharedSecretValue=myFooDefaultKey" />
</ConfigurationSettings>
</Role>
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(
CloudConfigurationManager.GetSetting(“ServiceBusConnectionString”));
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"))
{
namespaceManager.CreateQueue(qd);
// 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 =
QueueClient.CreateFromConnectionString(
myFooConnectionString, "FooQueue");
qc.Send(message);
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.

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(...

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 http://go.microsoft.com/fwlink/?LinkId=260882 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.51 EF 6.0.2 VS2013 It see...

Navigating Cloud Strategy after Azure Central US Region Outage

 Looking back, July 19, 2024, was challenging for customers using Microsoft Azure or Windows machines. Two major outages affected customers using CrowdStrike Falcon or Microsoft Azure computation resources in the Central US. These two outages affected many people and put many businesses on pause for a few hours or even days. The overlap of these two issues was a nightmare for travellers. In addition to blue screens in the airport terminals, they could not get additional information from the airport website, airline personnel, or the support line because they were affected by the outage in the Central US region or the CrowdStrike outage.   But what happened in reality? A faulty CrowdStrike update affected Windows computers globally, from airports and healthcare to small businesses, affecting over 8.5m computers. Even if the Falson Sensor software defect was identified and a fix deployed shortly after, the recovery took longer. In parallel with CrowdStrike, Microsoft provi...