Building your first Service Bus Brokered Messaging using Queue

Posted: March 16, 2013 in WCF, Windows Azure

Starting the first application working with the Service Bus Brokered Messaging, after well understanding the Microsoft.ServiceBus.Messaging namespace. Here is a simple function to work with the queues:

First of all you have to build the namespace on the Windows Azure Portal and link it to the application through the configuration setting like this example.

After that, we can start building an application that simply send and receive the Brokered Message through the Service Bus.

This function is simply to send a specified object through the Service Bus.

public WorkingWithQueues(string queuename)

{//initialization for the Service Bus and the configuration settingsvar
baseaddress = RoleEnvironment.GetConfigurationSettingValue(“namespaceAddress”);

var issuername = RoleEnvironment.GetConfigurationSettingValue(“issuername”);

var issuersecret = RoleEnvironment.GetConfigurationSettingValue(“issuersecret”);

//defining the namespace address

Uri namespaceaddress = ServiceBusEnvironment.CreateServiceUri(“sb”, baseaddress, string.Empty);
//creating the namespace manager and the queue

new NamespaceManager(
//initialization the messaging factory

//initialization of the queueclient

//Sending the Brokered Message of the class My Messages

namespaceaddress,TokenProvider.CreateSharedSecretTokenProvider(issuername, issuersecret))



Here are the main classes that control all the main instructions to the Service Bus.

BrokeredMessage: this is the unit of communication between the service bus clients. The message sent through the Brokered Message are objects or streams.

NamespaceManager: is responsible of the runtime operations no matter was the method used in Service Bus, Queue or Topic and Subscription.

MessagingFactory: is responsible of the messaging entity lifecycle whatever was its type, topic and Subscription or even the queue.

Certainly if you are using the queue in the Service Bus development, you will have to initialize the client for the use of queue. The QueueClient: this is the object that enables the sending and receiving the messages through the queue in Service Bus.

The following code is to receive a specified Object through the Service Bus. You will simply change the last line of code of the MessagingFactory to Receive like the following:

//initialization the messaging factory

//initialization of the queueclient

//Receiving the Brokered Message of the class My Messages

var x = MessagingFactory.Create(
namespaceaddress,TokenProvider.CreateSharedSecretTokenProvider(issuername,issuersecret)).CreateQueueClient(queuename).Receive(newTimeSpan(0, 3, 0));

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s