Archive

Archive for the ‘Service Bus’ Category

Writing a simple app to use Service Bus messaging

The Service Bus 1.0 for Windows Server is a really cool way to have a local instance of what Azure uses on the cloud for messaging. What’s cool about it? Well for one, you can write applications on your local box like you would for the cloud. So in other words, you can write applications that are cloud-ready and have them run on an on-premise environment too. For another, it is a great way to write loosely coupled applications based on messaging while leveraging the benefits of the underlying technology with transport reliability and availability.

I was enamored by the Service Bus enough to want to write a simple application to test its awesomeness but found guidance in several places, doing things in different ways in a somewhat disconnected fashion. So here’s my attempt at putting together a very basic application (or two) from scratch and doing the minimum work required to get messaging working. This test will use the Brokered Messaging capability of the Service Bus.

NOTE: All of the following was done on a single server installation of Service Bus 1.0.

Firstly, I wanted to create a new namespace that custom applications that I build to work with Service Bus can use called MyAppNamespace. I did this from the Service Bus PowerShell console using the following command

New-SBNamespace -Name MyAppNamespace -ManageUsers spdev\administrator

The -ManageUsers parameter is used to specify one or more user accounts that will have the ability to managed this namespace.

Then I used the following command to get the endpoint information for this new namespace I just created. This will be useful when I need to set up the connection string using which my application will need to connect to Service Bus.

Get-SBClientConfiguration -Namespaces MyAppNamespace

clip_image001

Then I launched Visual Studio and created a new console application project called MessageGenerator. I added reference to the following:

  1. Microsoft.ServiceBus.dll – which can be found typically at C:\Program Files\Service Bus\1.0.
  2. System.Configuration – needed to read the configuration from App.config
  3. System.Runtime.Serialization – needed by the namespace manager

clip_image002

Next, I brought up the App.config file for my application and added the following to it to define the connection string that the application can use to connect to the Service Bus. The value of the connection string appSetting is the one that is returned when Get-SBClientConfiguration is run from the Service Bus PowerShell for the namespace. It is not all visible here but the entire string was copied.

And while I was at this, I added another appSetting for the path (or the name) of the queue that I am going to create through my application.

clip_image003

In the Main function of my program, I set up the following to read from configuration and prepare the relevant strings

clip_image004

Next, I set up the console to

  1. Create a namespace manager
  2. Create a queue at the specified path if one doesn’t exist
  3. Spin up a queue client that will be used to send messages at the specified queue path
  4. Prompt user for messages until they no longer want to send one
  5. Read each message input by the user, create a brokered message and send it

Some exception handling is included to catch anything that may fail.

clip_image005

And finally (no pun intended), some clean up code.

clip_image006

Now since I have an application that sends messages, I created another quick console application that receives them called MessageReceiver. It is set up in exactly the same way as the sender in terms of settings and configuration. A couple of differences are

  1. This application does not need the System.Runtime.Serialization reference we added above since it doesn’t use the namespace manager to create a queue.
  2. As already mentioned above, it doesn’t use the namespace manager. It assumes that a queue exists at the specified path.

clip_image007

The main receiver code is a little different as seen below. There is no namespace manager here. The code

  1. Creates a client
  2. Connects it to the queue
  3. Receives a message every second
  4. Writes it to the console

And this goes on until the application is terminated by the user.

clip_image008

And the finally block

clip_image009

And that’s about it. Compiled and ran the applications and here’s the output.

clip_image010

Well there you go. Nothing really fancy in there but a really simple publish-subscribe model in action. You’ll need to be a geek to appreciate how cool this is. And a Pink Floyd fan to see what I did there. Happy messaging!

Advertisements
Categories: Service Bus Tags: ,
%d bloggers like this: