How-to: Messaging with Azure Queues
In this how-to i will explain with a sample application how we can do some messaging with Azure Queues.
First we need to create a Service Bus Namespace on the Azure Portal. For the moment we need to use the old portal.
We can’t do it on the new portal….
Click in the dashboard in the menu on the left on the “Service Bus” item, click then on the “Create a new namespace” link.
Then you should see the following screen.
Fill in an unique namespace name, the Region for deployment, the type ( we need the messaging for our queue ) also select the messaging Tier ( see the following link for the pricing ).
After some time your service bus namespace is available
You can click on the namespace, this will open the main dashboard.
Now we can start building the solution in Visual Studio (2013).
First we need to add the “ServiceBus” reference to our project. You can do this with the Nuget Package Manager.
You can also install it with the Nuget Package Manager Console. Just use the following command
Just copy the full connection string under the SAS heading to your “App.config”.
It should be something like this…
2:<configuration>
3:<startup>
4:<supportedRuntimeversion="v4.0"sku=".NETFramework,Version=v4.5"/>
5:</startup>
6:<system.serviceModel>
7:<extensions>
8:
9:<behaviorExtensions>
10:<addname="connectionStatusBehavior"type="Microsoft.ServiceBus.Configuration.ConnectionStatusElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
11:<addname="transportClientEndpointBehavior"type="Microsoft.ServiceBus.Configuration.TransportClientEndpointBehaviorElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
12:<addname="serviceRegistrySettings"type="Microsoft.ServiceBus.Configuration.ServiceRegistrySettingsElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
13:</behaviorExtensions>
14:<bindingElementExtensions>
15:<addname="netMessagingTransport"type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingTransportExtensionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
16:<addname="tcpRelayTransport"type="Microsoft.ServiceBus.Configuration.TcpRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
17:<addname="httpRelayTransport"type="Microsoft.ServiceBus.Configuration.HttpRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
18:<addname="httpsRelayTransport"type="Microsoft.ServiceBus.Configuration.HttpsRelayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
19:<addname="onewayRelayTransport"type="Microsoft.ServiceBus.Configuration.RelayedOnewayTransportElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
20:</bindingElementExtensions>
21:<bindingExtensions>
22:<addname="basicHttpRelayBinding"type="Microsoft.ServiceBus.Configuration.BasicHttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
23:<addname="webHttpRelayBinding"type="Microsoft.ServiceBus.Configuration.WebHttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
24:<addname="ws2007HttpRelayBinding"type="Microsoft.ServiceBus.Configuration.WS2007HttpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
25:<addname="netTcpRelayBinding"type="Microsoft.ServiceBus.Configuration.NetTcpRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
26:<addname="netOnewayRelayBinding"type="Microsoft.ServiceBus.Configuration.NetOnewayRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
27:<addname="netEventRelayBinding"type="Microsoft.ServiceBus.Configuration.NetEventRelayBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
28:<addname="netMessagingBinding"type="Microsoft.ServiceBus.Messaging.Configuration.NetMessagingBindingCollectionElement, Microsoft.ServiceBus, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
29:</bindingExtensions>
30:</extensions>
31:</system.serviceModel>
32:<appSettings>
33:
34:<addkey="Microsoft.ServiceBus.ConnectionString"value="Endpoint=sb://[your namespace].servicebus.windows.net;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=[your secret]"/>
35:</appSettings>
36:</configuration>
We use a couple methods, below i will describe them all..
The following method will create our queue. Also we delete the queue when it exists….
2: {
3: NamespaceManager namespaceManager = NamespaceManager.Create();
4: Console.WriteLine("\nCreating Queue '{0}'...", QueueName);
5:
6:// Delete the queue when it exists
7:if (namespaceManager.QueueExists(QueueName))
8: {
9: namespaceManager.DeleteQueue(QueueName);
10: }
11:
12: namespaceManager.CreateQueue(QueueName);
13: }
2: {
3: queueClient = QueueClient.Create(QueueName);
4:
5: BrokeredMessage message = new BrokeredMessage(string.Format("Sending message with Guid: {0}",Guid.NewGuid()));
6: message.MessageId = Guid.NewGuid().ToString();
7:
8: Console.WriteLine("\nSending message to Queue...");
9:
10:try
11: {
12: queueClient.Send(message);
13: }
14:catch (MessagingException e)
15: {
16:if (!e.IsTransient)
17: {
18: Console.WriteLine(e.Message);
19:throw;
20: }
21:else
22: {
23: HandleTransientErrors(e);
24: }
25: }
26: Console.WriteLine(string.Format("Message sent: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
27: Console.WriteLine();
28: }
2: {
3: Console.WriteLine("\nReceiving message from Queue...");
4: BrokeredMessage message = null;
5:while (true)
6: {
7:try
8: {
9://receive messages from Queue
10: message = queueClient.Receive(TimeSpan.FromSeconds(5));
11:if (message != null)
12: {
13: Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
14: Console.WriteLine();
15:// Further custom message processing could go here...
16: message.Complete();
17: }
18:else
19: {
20://no more messages in the queue
21:break;
22: }
23: }
24:catch (MessagingException e)
25: {
26:if (!e.IsTransient)
27: {
28: Console.WriteLine(e.Message);
29: Console.WriteLine();
30:throw;
31: }
32:else
33: {
34: HandleTransientErrors(e);
35: }
36: }
37: }
38: }
2:privatestatic QueueClient queueClient;
3:
4:staticvoid Main(string[] args)
5: {
6: Console.WriteLine("Creating a Queue");
7: CreateQueue();
8:while (true)
9: {
10: Console.WriteLine("Press S to start sending a message, Press R to receive messages, Press E to exit the console...");
11: ConsoleKeyInfo info = Console.ReadKey();
12:if (info.Key == ConsoleKey.S)
13: {
14://Sending Message
15: SendMessage();
16: }
17:elseif (info.Key == ConsoleKey.R)
18: {
19://Receive Messages
20: ReceiveMessages();
21: }
22:elseif (info.Key == ConsoleKey.E)
23: {
24://Exit the console & close the queue client
25: queueClient.Close();
26:break;
27: }
28: }
29: }
Have fun with it… If you have a problem. Ping me on twitter or send a comment on this post.