DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Supercharging AI Agents with Azure Context: A Hands-On Guide to Azure MCP
  • Building and Serving a Custom Model With Azure ML, Then Wiring It Into a Foundry Agent
  • A Deep Dive into the Microsoft Foundry Document Intelligence SDK: From PDF to Structured Data
  • AWS Bedrock vs Vertex AI vs Azure Foundry: Stop Comparing Benchmarks, Start Asking This Instead

Trending

  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • Choosing a BBD Framework
  • Claude Code Superpowers: The 5-Tool Stack That Ends Limits and Context Amnesia
  • Managing Technical Debt in AI-Generated Code

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Create a Service Bus With Windows Azure

How to Create a Service Bus With Windows Azure

Windows Azure Service Bus is a brokered, scalable, multi-featured messaging queuing system. It's a reliable message queuing and durable publish/subscribe system.

By 
Jitendra Bafna
·
Mar. 07, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
30.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In this article, we will walk through how to create a service bus the with Windows Azure portal.

Queue

  • The queue provides one-directional commmunication.

  • The queue processes the message in FIFO (first-in-first-out) order.

  • Messages in the queue can be consumed by one receiver.

  • Sender and receiver do not have to receive and send messages at the same time.

Topic

  • Like queues, topics also provide one-directional communication.

  • Topics can have subscriptions. One topic can have multiple subscriptions.

  • Subscriptions can optionally use a filter to only receive messages that match specific criteria.

How to Create a Service Bus With Windows Azure

Windows Azure Service Bus is a brokered, scalable, multi-featured messaging queueing system. It is a reliable message queuing and durable publish/subscribe system.

1. Create Service Bus Namespace

Log into the Azure Portal. In the left navigation pane, click New > Enterprise Integration > Service Bus.

Image title

In the Create Namespace dialog, provide the namespace name and the system will immediately check for namespaces that are availaible.

Select the Pricing Tier (basic, premium, and standard) and Subscription in which you need to create the service bus.

Select the existing Resource Group where your service bus will live or create a new one. Select the Location where you need to host your Service Bus and click Create.

Image title

2. Management Credentials

Click the newly created namespace from the list of service bus namespaces. Then, click Shared access policies > RootManageSharedAccessKey.

Image title

After clicking RootManageSharedAccessKey, you can see primary and secondary key and primary and secondary connection string. You can copy all these details for your future use.

Image title

3. Create Queue With Windows Azure

First, you need to ensure that the service bus namespace is already created. In the left pane of the portal, select the Service Bus in which you need to create a queue.

Select Queue and then click Add Queue.Image title

In the Create Queue Dialog, enter a queue name, select the max size and other properties depending on your requirements, and click Create.

Image title

4. Create Queue Using C# Code

// Store ConnectionString in web.config or app.config and copy connection string
// from service bus namespace
string connectionString =
 CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

var namespaceManager =
 NamespaceManager.CreateFromConnectionString(connectionString);

if (!namespaceManager.QueueExists("testqueue")) {
 namespaceManager.CreateQueue("testqueue");
}

5. Send Message to Queue (C# Code)

First, you need to install the nuget package WindowsAzure.ServiceBus. Right-click on your application in Visual Studio and select Manage NuGet Packages. Look for the Windows Azure Service Bus nuget package and install it.

using System;
using Microsoft.ServiceBus.Messaging;

namespace ServiceBusQueueSend {
 class Program {
  static void Main(string[] args) {
   var connectionString = "<Service Bus Namespace Primary or Secondary Connection String>";
   var queueName = "<Your queue name>";

   var client = QueueClient.CreateFromConnectionString(connectionString, queueName);
   var message = new BrokeredMessage("Test Message");

   client.Send(message);
  }
 }
}

Receiving message from queue:

using System;
using Microsoft.ServiceBus.Messaging;

namespace ServiceBusQueueReceive {
 class Program {
  static void Main(string[] args) {
   var connectionString = "<Service Bus Namespace Primary or Secondary Connection String>";
   var queueName = "QueueName";

   var client = QueueClient.CreateFromConnectionString(connectionString, queueName);

   client.OnMessage(message => {
    Console.WriteLine(String.Format("Message body: {0}", message.GetBody < String > ()));
    Console.WriteLine(String.Format("Message id: {0}", message.MessageId));
   });

   Console.ReadLine();
  }
 }
}

6. Create Topic With Windows Azure

First, you need to ensure that the service bus namespace is already created. In the left pane of the portal, look for the service bus in which you need to create a topic.

Select Topic and then click Add Topic.

Image title

In Create Topic Dialog, enter the topic name, select the max size and other properties depending on your requirements, and click Create.

Image title

Create topic using C#:

TopicDescription td = new TopicDescription("jittopic");
td.MaxSizeInMegabytes = 5120;
td.DefaultMessageTimeToLive = new TimeSpan(0, 1, 0);

// Create a new Topic with custom settings.
// Store ConnectionString in web.config or app.config and copy connection string
// from service bus namespace
string connectionString =
 CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

var namespaceManager =
 NamespaceManager.CreateFromConnectionString(connectionString);

if (!namespaceManager.TopicExists("jittopic")) {
 namespaceManager.CreateTopic(td);
}

Add subscription to topic:

string connectionString =
 CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

var namespaceManager =
 NamespaceManager.CreateFromConnectionString(connectionString);

if (!namespaceManager.SubscriptionExists("jittopic", "testsub")) {
 namespaceManager.CreateSubscription("jittopic", "testsub");
}

Add subscription with filters:

SqlFilter highIncomeFilter =
 new SqlFilter("income > 30000");

namespaceManager.CreateSubscription("jittopic",
 "HighIncome",
 highIncomeFilter);

SqlFilter lowIncomeFilter =
 new SqlFilter("income <= 30000");

namespaceManager.CreateSubscription("jittopic",
 "LowIncome",
 lowIncomeFilter);

Send message to topic:

using System;
using Microsoft.ServiceBus.Messaging;

namespace ServiceBusTpicSend {
 class Program {
  static void Main(string[] args) {
   var connectionString = "<Service Bus Namespace Primary or Secondary Connection String>";
   var topicName = "jittopic";

   var client = TopicClient.CreateFromConnectionString(connectionString, queueName);
   var message = new BrokeredMessage("Test Message");

   client.Send(message);
  }
 }
}

Receiving message from topic:

string connectionString =
 CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

SubscriptionClient Client =
 SubscriptionClient.CreateFromConnectionString(connectionString, "jittopic", "HighIncome");

// Configure the callback options.
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(1);

Client.OnMessage((message) => {
 try {
  // Process message from subscription.
  Console.WriteLine("\n*High Income");
  Console.WriteLine("Body: " + message.GetBody < string > ());
  Console.WriteLine("MessageID: " + message.MessageId);
  Console.WriteLine("Message Number: " +
   message.Properties["MessageNumber"]);

  message.Complete();
 } catch (Exception) {
  message.Abandon();
 }
}, options);

Now, you know how to create a service bus namespace, queue, and topic.

azure

Opinions expressed by DZone contributors are their own.

Related

  • Supercharging AI Agents with Azure Context: A Hands-On Guide to Azure MCP
  • Building and Serving a Custom Model With Azure ML, Then Wiring It Into a Foundry Agent
  • A Deep Dive into the Microsoft Foundry Document Intelligence SDK: From PDF to Structured Data
  • AWS Bedrock vs Vertex AI vs Azure Foundry: Stop Comparing Benchmarks, Start Asking This Instead

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook