Pushing Data to an Azure Service Bus Queue

This is a simple example of what you need to get data into a queue in Azure Service Bus from a console application. This uses the WindowsAzure.ServiceBus NuGet package, so you can actually use it anywhere you can store a connection string and write some .Net.

Go here to get started, or log into your Azure portal.

Setting up the Queue

First, we’re going to create a new Service Bus Queue through the portal. This is pretty easy, just select New –> App Services –> Queue and pick Custom Create.

image

Punch in a name, create a new namespace then click next. You can use the default settings, just hit confirm. Note that if you use Quick Create, it assumes you have a namespace already setup, if that’s the case, you can use that as well.

Navigate to the Namespace dashboard and go to the configure tab. You’ll need to note the shared access key name and the key value itself.

Building the App

Next, create a new Console App in Visual Studio. Add this configuration line to your App.config (just make sure to remove the whitespace, it wrecks your config):

    <add name=”AzureWebJobsServiceBus”
connectionString=
“Endpoint=sb://NAMESPACE.servicebus.windows.net/;
SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=KEY”
/>

This gives the framework the connection string you need via the conventionally named key/value pair. Punch in those details you grabbed from the portal so that the connection string is valid for your environment.

Next, add a simple person class to your project as such:

public class Person
{
public string Name { get; set; }
public string EmailAddress { get; set; }
}

Get The Bits You Need

Now, get ready to send the message. You want to add the following package to your program via the Package Manager Console (or the Manage Packages menu item by right-clicking on your project in Solution Explorer).

install-package WindowsAzure.ServiceBus

Put in the Code that Does the Heavy Lifting

Finally, update your program code to grab the connection string and publish the message to the queue.

static void Main(string[] args)
{
var connectionString = ConfigurationManager.ConnectionStrings[“AzureWebJobsServiceBus”].ConnectionString;
var client = QueueClient.CreateFromConnectionString(connectionString, “NAME-OF-QUEUE”);
var person = new Person {Name = “James Chambers”, EmailAddress = james@foo.com};
client.Send(new BrokeredMessage(person));
}

That’s it. Four lines of code and you have a way to push out a message and get back to work. Heck, if you can await (as in, in an aysnc method) you can even use SendAsync instead of Send so you don’t tie up your thread.

Next Steps

In the next post I’ll show you how to pop stuff off the queue and do something with it, then we’ll have a look at how to debug all of this.

Make sure you pop into Azure and give this a try! There is an amount of free credits that you get during your trial period. If you want to extend that, consider getting into MSDN (or getting your boss to) for other cool things as well, like VS Online and access to over a terabyte of development assets & software.

Happy Coding! Smile