Send a Brokered Message to Azure Service Bus

Introduction

In this tutorial, we will write a simple C# program to send a brokered message to the Azure Service Bus. The tools that we'll leverage are Visual Studio 2013, .NET Framework 4.5.2, Microsoft Azure Service Bus client library from NuGet, along with Azure Service Bus namespace, topic and subscription on the Azure Portal.

Step 1: Create a Console Application

Create a Console Application using .NET Framework 4.5.2 and name the solution AzureServiceBus.



Step 2: Setup Namespace, Topic and Subscription on Azure Service Bus

Log into the Azure portal and navigate to the Service Bus as outlined below.

Now create a namespace (for example testnamespace), a topic (for example testtopic) and a subscription (for example testsubscription1) and verify the configuration elements as in the following:

  1. Base Address: "sb://testnamespace.servicebus.windows.net/"
  2. Key Name: "RootManageSharedAccessKey"
  3. Access Key: "zjoqxtDex7Bx3qXIXuedOiErinCOcnTPQ7aoEUhmelA="
  4. Topic Name: "testtopic"

Step 3: Create Sender

The code excerpt to create a sender is outlined below. A token provider is created using key name and access key values. A Messaging Factory object is created by providing a token provider and baseaddress. The sender is created using a messaging factory object.

  1. string keyName = "RootManageSharedAccessKey";  
  2. string accessKey = "cjoqxtCex8Bx3qXIXuedOiZrinCOcnTNP7aoEUhmelA=";  
  3. string baseAddress = "sb://ageroenterprise-dev.servicebus.windows.net/";  
  4. string topicName = "testtopic";   
  5.   
  6. // Create Sender  
  7. TokenProvider tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(keyName, accessKey);  
  8. MessagingFactory messagingFactory = MessagingFactory.Create(baseAddress, tokenProvider);  
  9. MessageSender sender = messagingFactory.CreateMessageSender(topicName); 

Also, please note that the token can be created with expiry duration passing TimeSpan input. Feel free to explore the TokenProviderClass in additional detail in MSDN.

Step 4: Build an Object to Send

For this step, let's consider a simple employeeobject with three fields (EmpId, EmpName and Date Of Hire) to show the process and populate the object with sample values as in the following.

  1. namespace AzureServiceBus.Model  
  2. {  
  3.     [DataContract]  
  4.     public class Employee  
  5.     {  
  6.         [DataMember(Name = "empId")]  
  7.         public string EmpId { getset; }  
  8.         [DataMember(Name = "empName")]  
  9.         public string EmpName { getset; }  
  10.         [DataMember(Name = "dateOfHire")]  
  11.         public DateTime DateOfHire { getset; }  
  12.     }  
  13. }  
  14.   
  15. // Prepare message  
  16. Model.Employee employee = new Employee();  
  17. employee.EmpId = "1001";  
  18. employee.EmpName = "David";  
  19. employee.DateOfHire = new DateTime(1973, 8, 5); 
Step 5: Send Message

Send a brokered message using the sender object, in which the brokered message body is filled with a custom object. Calling the send()method will drop your message on the Azure Service Bus and can be verified on a subscription.

  1. // Send  
  2. sender.Send(new BrokeredMessage(employee));  
  3. Console.WriteLine("Message sent successfully.");  
  4. Console.ReadLine(); 

Up Next
    Ebook Download
    View all
    TOP Azure
    Read by 1 people
    Download Now!
    Learn
    View all