Skip to content
Skillpedia
APIs & servers (backend)48k Β· #3986

azure-servicebus-dotnet

What it does

This code allows computer programs to send and receive messages reliably. Think of it as an advanced mail system for different parts of an application to communicate.

When to use it

  • βœ“When you need to ensure a message isn't lost.
  • βœ“To organize communication between different parts of a system.
  • βœ“When one program needs to send data to another securely.

Skill link

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/azure-servicebus-dotnet/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/azure-servicebus-dotnet/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute β†’

Example of what to ask your AI

β€œSend a 'Hello World' message to the queue 'my-queue'.”

5 new skills a week. 1 minute to read.

How it works

The code connects to a messaging service called Azure Service Bus. It lets you create 'senders' to send messages and 'receivers' to get them, ensuring everything reaches its destination.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

Azure.Messaging.ServiceBus (.NET)

Enterprise messaging SDK for reliable message delivery with queues, topics, subscriptions, and sessions.

Installation

dotnet add package Azure.Messaging.ServiceBus
dotnet add package Azure.Identity

Current Version: v7.20.1 (stable)

Environment Variables

AZURE_SERVICEBUS_FULLY_QUALIFIED_NAMESPACE=<namespace>.servicebus.windows.net
# Or connection string (less secure)
AZURE_SERVICEBUS_CONNECTION_STRING=Endpoint=sb://...

Authentication

Microsoft Entra ID (Recommended)

using Azure.Identity;
using Azure.Messaging.ServiceBus;

string fullyQualifiedNamespace = "<namespace>.servicebus.windows.net";
await using ServiceBusClient client = new(fullyQualifiedNamespace, new DefaultAzureCredential());

Connection String

string connectionString = "<connection_string>";
await using ServiceBusClient client = new(connectionString);

ASP.NET Core Dependency Injection

services.AddAzureClients(builder =>
{
    builder.AddServiceBusClientWithNamespace("<namespace>.servicebus.windows.net");
    builder.UseCredential(new DefaultAzureCredential());
});

Client Hierarchy

ServiceBusClient
β”œβ”€β”€ CreateSender(queueOrTopicName)      β†’ ServiceBusSender
β”œβ”€β”€ CreateReceiver(queueName)           β†’ ServiceBusReceiver
β”œβ”€β”€ CreateReceiver(topicName, subName)  β†’ ServiceBusReceiver
β”œβ”€β”€ AcceptNextSessionAsync(queueName)   β†’ ServiceBusSessionReceiver
β”œβ”€β”€ CreateProcessor(queueName)          β†’ ServiceBusProcessor
└── CreateSessionProcessor(queueName)   β†’ ServiceBusSessionProcessor

ServiceBusAdministrationClient (separate client for CRUD)

Core Workflows

1. Send Messages

await using ServiceBusClient client = new(fullyQualifiedNamespace, new DefaultAzureCredential());
ServiceBusSender sender = client.CreateSender("my-queue");

// Single message
ServiceBusMessage message = new("Hello world!");
await sender.SendMessageAsync(message);

// Safe batching (recommended)
using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync();
if (batch.TryAddMessage(new ServiceBusMessage("Message 1")))
{
    // Message added successfully
}
if (batch.TryAddMessage(new ServiceBusMessage("Message 2")))
{
    // Message added successfully
}
await sender.SendMessagesAsync(batch);

2. Receive Messages

ServiceBusReceiver receiver = client.CreateReceiver("my-queue");

// Single message
ServiceBusReceivedMessage message = await receiver.ReceiveMessageAsync();
string body = message.Body.ToString();
Console.WriteLine(body);

// Complete the message (removes from queue)
await receiver.CompleteMessageAsync(message);

// Batch receive
IReadOnlyList<ServiceBusReceivedMessage> messages = await receiver.ReceiveMessagesAsync(maxMessages: 10);
foreach (var msg in messages)
{
    Console.WriteLine(msg.Body.ToString());
    await receiver.CompleteMessageAsync(msg);
}

3. Message Settlement

// Complete - removes message from queue
await receiver.CompleteMessageAsync(message);

// Abandon - releases lock, message can be received again
await receiver.AbandonMessageAsync(message);

// Defer - prevents normal receive, use ReceiveDeferredMessageAsync
await receiver.DeferMessageAsync(message);

// Dead Letter - moves to dead letter subqueue
await receiver.DeadLetterMessageAsync(message, "InvalidFormat", "Message body was not valid JSON");

4. Background Processing with Processor

ServiceBusProcessor processor = client.CreateProcessor("my-queue", new ServiceBusProcessorOptions
{
    AutoCompleteMessages = false,
    MaxConcurrentCalls = 2
});

processor.ProcessMessageAsync += async (args) =>
{
    try
    {
        string body = args.Message.Body.ToString();
        Console.WriteLine($"Received: {body}");
        await args.CompleteMessageAsync(args.Message);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error processing: {ex.Message}");
        await args.AbandonMessageAsync(args.Message);
    }
};

processor.ProcessErrorAsync += (args) =>
{
    Console.WriteLine($"Error source: {args.ErrorSource}");
    Console.WriteLine($"Entity: {args.EntityPath}");
    Console.WriteLine($"Exception: {args.Exception}");
    return Task.CompletedTask;
};

await processor.StartProcessingAsync();
// ... application runs
await processor.StopProcessingAsync();

5. Sessions (Ordered Processing)

// Send session message

…
FAQ

What are queues and topics?

Queues are like mailboxes where messages wait until received. Topics are like bulletin boards where messages are broadcast to multiple subscribers.

How are messages guaranteed?

The system ensures messages are delivered. If a receiver fails, the message can be retried or placed in a 'dead-letter queue' for inspection.

What is 'background processing'?

It's a way for the program to continuously receive and process messages without being asked each time, making communication more efficient.

Details

Original description by the author

Azure Service Bus SDK for .NET. Enterprise messaging with queues, topics, subscriptions, and sessions.

repo: sickn33/agentic-awesome-skills

Popularity

GitHub stars
48k
Ranking position
#3986
License
MIT
Updated
10/15/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills