Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Integrating Azure Service Bus with ASP.NET Core: Deep Dive into Queues, Topics, and Subscriptions

Integrating Azure Service Bus with ASP.NET Core: Deep Dive into Queues, Topics, and Subscriptions

Date- May 10,2026 215
azure service bus

Overview

Azure Service Bus is a fully managed enterprise integration message broker that facilitates communication between different applications and services. It primarily exists to enable decoupled microservices architectures, allowing systems to exchange messages asynchronously, which is essential for building resilient and scalable applications. By leveraging the capabilities of Azure Service Bus, developers can offload workloads, manage traffic spikes, and ensure reliable message delivery.

This messaging service offers several features, including queues, topics, and subscriptions, which cater to various messaging patterns. Queues are designed for point-to-point communication, where a single sender communicates with a single receiver. In contrast, topics and subscriptions enable publish-subscribe patterns, allowing multiple subscribers to receive messages from a single publisher, thus facilitating more complex workflows and integrations.

Real-world use cases for Azure Service Bus include order processing systems, event-driven architectures, and decoupled application components in microservices. For instance, an e-commerce platform can use Azure Service Bus to manage orders by sending messages to a queue that triggers further processing, such as inventory updates and payment processing, ensuring that these tasks are handled asynchronously without blocking the main application flow.

Prerequisites

  • ASP.NET Core Knowledge: Familiarity with ASP.NET Core framework and its components.
  • Azure Subscription: An active Azure account to create and manage Azure Service Bus resources.
  • NuGet Package Manager: Understanding how to install and manage NuGet packages in ASP.NET Core projects.
  • Basic C# Skills: Proficiency in C# programming to understand and implement code examples.

Setting Up Azure Service Bus

Before diving into code, you need to set up an Azure Service Bus namespace and create the necessary entities (queues, topics, and subscriptions). This involves accessing the Azure Portal and following a series of steps to configure your messaging infrastructure.

To create a Service Bus namespace, log into the Azure Portal, navigate to the "Service Bus" section, and click on "+ Create." Fill in the required fields such as subscription, resource group, and namespace name. Once the namespace is created, you can create queues or topics as per your application needs.

// Sample code to create a queue in Azure Service Bus using Azure.Messaging.ServiceBus package
using Azure.Messaging.ServiceBus;

var client = new ServiceBusAdministrationClient("");
await client.CreateQueueAsync("myqueue");

This code snippet demonstrates how to programmatically create a queue named "myqueue" using the ServiceBusAdministrationClient class from the Azure.Messaging.ServiceBus library. The connection string is obtained from the Azure Portal under your Service Bus namespace.

Code Explanation

The above code follows these steps:

  1. Import the Azure.Messaging.ServiceBus namespace to access the necessary classes for interacting with Azure Service Bus.
  2. Create an instance of ServiceBusAdministrationClient using your Azure Service Bus connection string.
  3. Call CreateQueueAsync method to create a new queue with the specified name.

Sending Messages to a Queue

After setting up your queue, the next step is to send messages to it. Azure Service Bus supports sending messages in various formats, allowing you to customize the message payload according to your application's requirements.

// Sending a message to the queue
using Azure.Messaging.ServiceBus;

var client = new ServiceBusClient("");
var sender = client.CreateSender("myqueue");

var message = new ServiceBusMessage("Hello, Azure Service Bus!");
await sender.SendMessageAsync(message);

await sender.DisposeAsync();

This code snippet demonstrates how to send a message to the previously created queue. It uses the ServiceBusClient class to create a sender for the queue and sends a message containing the text "Hello, Azure Service Bus!".

Code Explanation

The code performs the following actions:

  1. Create an instance of ServiceBusClient using the connection string to connect to the Azure Service Bus.
  2. Use the CreateSender method to obtain a sender for the specified queue.
  3. Create a new ServiceBusMessage instance with the desired message content.
  4. Invoke SendMessageAsync to send the message to the queue.
  5. Dispose of the sender instance to release resources.

Receiving Messages from a Queue

Receiving messages from an Azure Service Bus queue can be achieved using either the ReceiveMessagesAsync method for batch processing or the ServiceBusProcessor for event-driven processing. The former is suitable for scenarios where you want to pull messages at your convenience, while the latter is ideal for real-time applications.

// Receiving messages from the queue
using Azure.Messaging.ServiceBus;

var client = new ServiceBusClient("");
var processor = client.CreateProcessor("myqueue");

processor.ProcessMessageAsync += async args => {
    var body = args.Message.Body.ToString();
    Console.WriteLine($"Received: {body}");
    await args.CompleteMessageAsync(args.Message);
};

processor.ProcessErrorAsync += async args => {
    Console.WriteLine($"Error: {args.Exception.Message}");
};

await processor.StartProcessingAsync();

This snippet shows how to set up a message processor that listens for incoming messages on the specified queue.

Code Explanation

This code handles message processing as follows:

  1. Create an instance of ServiceBusClient using your connection string.
  2. Use CreateProcessor to instantiate a message processor for the specified queue.
  3. Subscribe to the ProcessMessageAsync event to handle received messages. Here, we read the message body and print it to the console before completing the message.
  4. Subscribe to the ProcessErrorAsync event to handle any errors that may occur during processing.
  5. Start processing messages by calling StartProcessingAsync.

Using Topics and Subscriptions

Topics and subscriptions provide a powerful way to implement the publish-subscribe messaging pattern in Azure Service Bus. This is particularly useful for scenarios where multiple consumers need to receive the same message independently.

To set up a topic, follow similar steps as creating a queue but choose "Topics" instead. Each topic can have multiple subscriptions, allowing different consumers to process the same messages based on their business logic.

// Creating a topic and subscription
var adminClient = new ServiceBusAdministrationClient("");
await adminClient.CreateTopicAsync("mytopic");
await adminClient.CreateSubscriptionAsync("mytopic", "mysubscription");

This code snippet demonstrates how to create a topic named "mytopic" and a subscription named "mysubscription".

Code Explanation

Here’s how the code works:

  1. Create an instance of ServiceBusAdministrationClient with your connection string.
  2. Call CreateTopicAsync to create a new topic.
  3. Call CreateSubscriptionAsync to create a subscription under the specified topic.

Publishing Messages to a Topic

Once you have a topic and subscription setup, you can publish messages to the topic. All subscriptions linked to the topic will receive messages independently, allowing for flexible message handling.

// Publishing a message to a topic
var client = new ServiceBusClient("");
var sender = client.CreateSender("mytopic");

var message = new ServiceBusMessage("Hello, Subscribers!");
await sender.SendMessageAsync(message);

await sender.DisposeAsync();

This code snippet illustrates how to send a message to a topic.

Code Explanation

The publishing process includes:

  1. Creating a ServiceBusClient instance with the connection string.
  2. Creating a sender for the topic using CreateSender.
  3. Creating a new message with the desired content.
  4. Sending the message to the topic using SendMessageAsync.
  5. Disposing of the sender instance.

Receiving Messages from a Subscription

To receive messages from a subscription, you can use the same processing model as with queues, allowing you to handle messages from subscriptions in an event-driven manner.

// Receiving messages from a subscription
var client = new ServiceBusClient("");
var processor = client.CreateProcessor("mytopic", "mysubscription");

processor.ProcessMessageAsync += async args => {
    var body = args.Message.Body.ToString();
    Console.WriteLine($"Received from subscription: {body}");
    await args.CompleteMessageAsync(args.Message);
};

processor.ProcessErrorAsync += async args => {
    Console.WriteLine($"Error: {args.Exception.Message}");
};

await processor.StartProcessingAsync();

This snippet shows how to set up a processor for a specific subscription.

Code Explanation

The subscription message processing is similar to queue processing:

  1. Instantiate a ServiceBusClient object using your connection string.
  2. Create a processor for the topic and subscription with CreateProcessor.
  3. Handle incoming messages in the ProcessMessageAsync event.
  4. Handle errors in the ProcessErrorAsync event.
  5. Start message processing using StartProcessingAsync.

Edge Cases & Gotchas

When working with Azure Service Bus, developers may encounter several pitfalls that can lead to unexpected behaviors.

Common Pitfalls

  • Message Lock Expiration: Messages in a queue or subscription can be locked for processing. If processing takes longer than the lock duration, the message becomes visible again which can lead to duplicate processing. Always ensure that your processing logic completes within the lock duration or extend the lock if necessary.
  • Dead-letter Queue Mismanagement: If a message fails to process multiple times, it is moved to a dead-letter queue. Ensure to monitor and handle dead-letter messages appropriately to avoid losing important data.
  • Connection String Exposure: Be cautious with your connection strings. Store them securely using Azure Key Vault or environment variables to avoid exposing sensitive information in your code.

Performance & Best Practices

To optimize your Azure Service Bus integration, consider the following best practices:

Message Batching

Sending messages in batches rather than one at a time can significantly enhance throughput. Azure Service Bus allows you to send multiple messages in a single API call.

// Sending messages in a batch
var batch = await sender.CreateMessageBatchAsync();

foreach (var messageContent in new[] { "Message 1", "Message 2", "Message 3" }) {
    var message = new ServiceBusMessage(messageContent);
    if (!batch.TryAddMessage(message)) {
        // Handle batch full scenario
    }
}
await sender.SendMessagesAsync(batch);

This example shows how to create a message batch and add multiple messages to it before sending.

Connection Management

Reuse the ServiceBusClient instance across your application lifecycle instead of creating a new instance for each operation. This reduces connection overhead and improves performance.

Monitoring and Logging

Implement logging and monitoring to track message processing times, failures, and dead-letter messages. Azure Monitor and Application Insights can assist in keeping track of your Service Bus metrics.

Real-World Scenario

Let's consider a simple ASP.NET Core application that processes orders using Azure Service Bus. The application will consist of two main components: an order service that sends order messages to a queue, and a processing service that receives and processes these messages.

Order Service

public class OrderService {
    private readonly ServiceBusClient _client;
    public OrderService(string connectionString) {
        _client = new ServiceBusClient(connectionString);
    }
    public async Task PlaceOrderAsync(string orderId) {
        var sender = _client.CreateSender("orderqueue");
        var message = new ServiceBusMessage(orderId);
        await sender.SendMessageAsync(message);
        await sender.DisposeAsync();
    }
}

Processing Service

public class OrderProcessor {
    private readonly ServiceBusClient _client;
    public OrderProcessor(string connectionString) {
        _client = new ServiceBusClient(connectionString);
    }
    public async Task StartProcessingAsync() {
        var processor = _client.CreateProcessor("orderqueue");
        processor.ProcessMessageAsync += async args => {
            Console.WriteLine($"Processing order: {args.Message.Body}");
            await args.CompleteMessageAsync(args.Message);
        };
        await processor.StartProcessingAsync();
    }
}

Application Startup

public class Startup {
    public void ConfigureServices(IServiceCollection services) {
        services.AddSingleton(provider => new OrderService(""));
        services.AddSingleton(provider => new OrderProcessor(""));
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
        var processor = app.ApplicationServices.GetService();
        processor.StartProcessingAsync();
    }
}

In this mini-project, the OrderService class is responsible for sending messages to the queue, while the OrderProcessor class listens for and processes these messages. The Startup class configures the services and starts the message processor when the application runs.

Conclusion

  • Azure Service Bus is a powerful tool for building decoupled and resilient applications through asynchronous messaging.
  • Understanding queues, topics, and subscriptions is essential for implementing effective messaging patterns.
  • Best practices such as message batching, connection management, and robust monitoring can significantly enhance your application's performance.
  • Always consider edge cases and potential pitfalls to avoid common mistakes when working with Azure Service Bus.
  • Next steps include exploring Azure Functions for serverless processing or looking into Azure Event Grid for event-driven architectures.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

CWE-798: Managing Secrets in ASP.NET Core with User Secrets and Azure Key Vault
May 31, 2026
Integrating RabbitMQ with ASP.NET Core Using MassTransit: A Complete Guide
May 10, 2026
Implementing Microsoft Azure AD Authentication for Enterprise SSO in ASP.NET Core Applications
Apr 30, 2026
CWE-319: Enforcing HTTPS and HSTS in ASP.NET Core Applications
Apr 28, 2026
Previous in ASP.NET Core
Integrating RabbitMQ with ASP.NET Core Using MassTransit: A Compl…
Next in ASP.NET Core
Integrating AWS SQS and SNS in ASP.NET Core for Decoupled Microse…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 231 views
  • 2
    CWE-269: Improper Privilege Management - Implementing the … 248 views
  • 3
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,887 views
  • 4
    Error-An error occurred while processing your request in .… 11,922 views
  • 5
    Mastering Unconditional Statements in C: A Complete Guide … 22,166 views
  • 6
    How to Connect to a Database with MySQL Workbench 8,350 views
  • 7
    How to create a read-only MySQL user 11,053 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26668 views
  • Exception Handling Asp.Net Core 21692 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 21140 views
  • How to implement Paypal in Asp.Net Core 20115 views
  • Task Scheduler in Asp.Net core 18188 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor