Currently fiddling with the Service Bus and Queues. In the process of seeing if this would be a better way to send data to Azure. So how does one programmatically communicate with a Service Bus Queue?
There are a few things that one would need.
- A Microsoft Azure subscription. For this, go to www.windowsazure.com and subscribe.
- A Service Bus and Queue (although I will need to create the queue programmatically)
- Visual Studio and some C# skills.
So, what will be done is to create a Service Bus on your azure subscription.
And that bus will have its properties, take note of the connection string.
Then fire up Visual Studio and create a new project. A console project will do. Nothing fancy here, just need to establish communication to the Queue by sending some stuff and then retrieving them right after.
You will need to add some references to access the ServiceBus namespace J
And once you have those in place, you can proceed to coding this out. Now would be a good time to fix up the references
So, what object would you need to “talk” to the Queue? Well, common sense would dictate that you would need a “message” to send before attempting to talk to the queue right? It appears to be a bit of too many pieces, but these could all be “managed” right? So.. Microsoft.ServiceBus.NamespaceManager would do us well..
How to programmatically create a Queue on Microsoft Azure using C#.
I’ll go ahead and create a separate method to programmatically create a Queue on my Azure Subscription using C#.
NamespaceManager MyManager = NamespaceManager.Create();
This is simple.. too simple. (always wanted to say that).
And some searching will reveal that there are some other things that one would need to make this work, but sometimes I just like to play around and hit F5 to get to see what I need to do.
Running the app and calling the method should give you this: (not exactly.. there is a surprise here that I am sure you can fix on your own)
“Configuration is missing required information. Make sure the property ‘Endpoint’ is defined as part of ‘Microsoft.ServiceBus.ConnectionString’ key within ‘appSettings’ section, or Windows Azure configuration settings.”
This is as straightforward as it can be. You need to add the servicebus connection string in you appSettings J So..
Retrieving the settings:
Adding it to app.config:
Should allow us to run it without doing anything else :D
So basically, we have created a way to create a Queue, where will this queue be created? In the Azure Subscription identified by the connection string. So running this would result in..
A newly created queue, programmatically, with nothing in it. LOL. Anyways, it worked.
Do take some time to investigate. Did you notice that the casing of the queue’s name was converted to lower case?
Looking at the documentation, upper case characters should be allowed. Let us dig into that some other time.
Send Message to Azure ServiceBus Queue using C#
So now we have a queue; how do we send message objects?
The QueueClient object should do well here. So I make a separate method to send a simple message object of type BrokeredMessage.
QueueClient MyQueueClient = QueueClient.Create(“testqueue”);
BrokeredMessage MyMessage = new
BrokeredMessage(“this is a test message”);
MyMessage.MessageId = “1”;
Very very straightforward and devoid of any validation J
Create a message, send the message. That’s it.
It says the length now is 1, because the app has sent one message J
How to retrieve an azure servicebus queue message using C#
So now let us retrieve the message and its contents.
I had to step into the code to show the retrieved message.. too lazy to go Console.Writeline. LOL!
So there you have it.. your basic of sending and retrieving messages to the Microsoft Azure ServiceBus Queue. The items you see here is by all means the simplest and most happy programming available. If one follows this step by step, one will encounter an error that should easily be resolved J
Also, I will leave it up to the reader to:
- Add validation to the methods. Best practice would dictate to see first if the Queue exists before creating it.
- Refactor the code for better performance.